-
Notifications
You must be signed in to change notification settings - Fork 238
/
test_qiskit.py
182 lines (139 loc) · 4.52 KB
/
test_qiskit.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
173
174
175
176
177
178
179
180
181
182
import qiskit
from qiskit import quantum_info
from qiskit.execute_function import execute
from qiskit import BasicAer
import numpy as np
import pickle
import json
import os
import sys
from collections import Counter
from sklearn.metrics import mean_squared_error
from typing import Dict, List
import matplotlib.pyplot as plt
if len(sys.argv) > 1:
data_path = sys.argv[1]
else:
data_path = '.'
#define utility functions
def simulate(circuit: qiskit.QuantumCircuit) -> dict:
"""Simulate the circuit, give the state vector as the result."""
backend = BasicAer.get_backend('statevector_simulator')
job = execute(circuit, backend)
result = job.result()
state_vector = result.get_statevector()
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: qiskit.QuantumCircuit) -> Dict[int, int]:
"""Returns the number of gate operations with each number of qubits."""
counter = Counter([len(gate[1]) for gate in circuit.data])
#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)
image_re = np.asarray(image_re)
#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):
q = qiskit.QuantumRegister(3)
circuit = qiskit.QuantumCircuit(q)
if image[0][0]==0:
circuit.rx(np.pi,0)
return circuit
def decode(histogram):
if 1 in histogram.keys():
image=[[0,0],[0,0]]
else:
image=[[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
classifier=qiskit.QuantumCircuit.from_qasm_file('quantum_classifier.qasm')
#encode image into circuit
circuit=encode(image)
#append with classifier circuit
nq1 = circuit.width()
nq2 = classifier.width()
nq = max(nq1, nq2)
qc = qiskit.QuantumCircuit(nq)
qc.append(circuit.to_instruction(), list(range(nq1)))
qc.append(classifier.to_instruction(), list(range(nq2)))
#simulate circuit
histogram=simulate(qc)
#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()