forked from Eirenne/2023_IonQ_Remote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
172 lines (128 loc) · 4.16 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import cirq
import numpy as np
import pickle
import json
import os
import sys
from collections import Counter
from sklearn.metrics import mean_squared_error
if len(sys.argv) > 1:
data_path = sys.argv[1]
else:
data_path = '.'
#define utility functions
def simulate(circuit: cirq.Circuit) -> dict:
"""This function simulates a Cirq circuit (without measurement) and outputs results in the format of histogram.
"""
simulator = cirq.Simulator()
result = simulator.simulate(circuit)
state_vector=result.final_state_vector
histogram = dict()
for i in range(len(state_vector)):
population = abs(state_vector[i]) ** 2
if population > 1e-9:
histogram[i] = population
return histogram
def histogram_to_category(histogram):
"""This function takes a histogram representation of circuit execution results, and processes into labels as described in
the problem description."""
assert abs(sum(histogram.values())-1)<1e-8
positive=0
for key in histogram.keys():
digits = bin(int(key))[2:].zfill(20)
if digits[-1]=='0':
positive+=histogram[key]
return positive
def count_gates(circuit: cirq.Circuit):
"""Returns the number of 1-qubit gates, number of 2-qubit gates, number of 3-qubit gates...."""
counter=Counter([len(op.qubits) for op in circuit.all_operations()])
#feel free to comment out the following two lines. But make sure you don't have k-qubit gates in your circuit
#for k>2
for i in range(2,20):
assert counter[i]==0
return counter
def image_mse(image1,image2):
# Using sklearns mean squared error:
# https://scikit-learn.org/stable/modules/generated/sklearn.metrics.mean_squared_error.html
return mean_squared_error(255*image1,255*image2)
def test():
#load the actual hackthon data (fashion-mnist)
images=np.load(data_path+'/images.npy')
labels=np.load(data_path+'/labels.npy')
#test part 1
n=len(images)
mse=0
gatecount=0
for image in images:
#encode image into circuit
circuit,image_re=run_part1(image)
#count the number of 2qubit gates used
gatecount+=count_gates(circuit)[2]
#calculate mse
mse+=image_mse(image,image_re)
#fidelity of reconstruction
f=1-mse/n
gatecount=gatecount/n
#score for part1
score_part1=f*(0.999**gatecount)
#test part 2
score=0
gatecount=0
n=len(images)
for i in range(n):
#run part 2
circuit,label=run_part2(images[i])
#count the gate used in the circuit for score calculation
gatecount+=count_gates(circuit)[2]
#check label
if label==labels[i]:
score+=1
#score
score=score/n
gatecount=gatecount/n
score_part2=score*(0.999**gatecount)
print(score_part1, ",", score_part2, ",", data_path, sep="")
############################
# YOUR CODE HERE #
############################
def encode(image):
circuit=cirq.Circuit()
if image[0][0]==0:
circuit.append(cirq.rx(np.pi).on(cirq.LineQubit(0)))
return circuit
def decode(histogram):
if 1 in histogram.keys():
image=np.array([[0,0],[0,0]])
else:
image=np.array([[1,1],[1,1]])
return image
def run_part1(image):
#encode image into a circuit
circuit=encode(image)
#simulate circuit
histogram=simulate(circuit)
#reconstruct the image
image_re=decode(histogram)
return circuit,image_re
def run_part2(image):
# load the quantum classifier circuit
with open('quantum_classifier.pickle', 'rb') as f:
classifier=pickle.load(f)
#encode image into circuit
circuit=encode(image)
#append with classifier circuit
circuit.append(classifier)
#simulate circuit
histogram=simulate(circuit)
#convert histogram to category
label=histogram_to_category(histogram)
#thresholding the label, any way you want
if label>0.5:
label=1
else:
label=0
return circuit,label
############################
# END YOUR CODE #
############################
test()