-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdateset.py
187 lines (157 loc) · 5.54 KB
/
dateset.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
183
184
185
186
187
from __future__ import annotations
import os
import numpy as np
import random
from fractions import Fraction
from qiskit import AncillaRegister, ClassicalRegister, QuantumCircuit, QuantumRegister, qasm2
from qiskit.circuit.library import QFT
from qiskit_algorithms import Grover
from qiskit.circuit.library import GroverOperator
from math import pi
def h_0(n) -> QuantumCircuit:
qc = QuantumCircuit(n)
for i in range(n):
qc.h(0)
return qc
def h_c(n) -> QuantumCircuit:
qc = QuantumCircuit(n)
for i in range(n):
qc.h(i)
return qc
def gen_ghz(n) -> QuantumCircuit:
qc = QuantumCircuit(n)
qc.h(0)
for i in range(1,n):
qc.cx(0, i)
return qc
def rx_c(n) -> QuantumCircuit:
qc = QuantumCircuit(n)
angle = pi
for i in range(n):
qc.rx(angle, i)
angle /= 2
return qc
def rx_gradually_c(n) -> QuantumCircuit:
qc = QuantumCircuit(n)
angle = pi
for level in range(1, n + 1):
current_angle = angle / (2 ** (level - 1))
for i in range(level):
qc.rx(current_angle, i)
return qc
def qft(num_qubits: int) -> QuantumCircuit:
"""Returns a quantum circuit implementing the Quantum Fourier Transform algorithm.
Keyword arguments:
num_qubits -- number of qubits of the returned quantum circuit
"""
q = QuantumRegister(num_qubits, "q")
c = ClassicalRegister(num_qubits, "c")
qc = QuantumCircuit(q, c, name="qft")
qc.compose(QFT(num_qubits=num_qubits), inplace=True)
qc.measure_all()
return qc
def qpe(num_qubits: int) -> QuantumCircuit:
"""Returns a quantum circuit implementing the Quantum Phase Estimation algorithm for a phase which can be
exactly estimated.
Keyword arguments:
num_qubits -- number of qubits of the returned quantum circuit
"""
num_qubits = num_qubits - 1 # because of ancilla qubit
q = QuantumRegister(num_qubits, "q")
psi = QuantumRegister(1, "psi")
c = ClassicalRegister(num_qubits, "c")
qc = QuantumCircuit(q, psi, c, name="qpeexact")
# get random n-bit string as target phase
random.seed(10)
theta = 0
while theta == 0:
theta = random.getrandbits(num_qubits)
lam = Fraction(0, 1)
# print("theta : ", theta, "correspond to", theta / (1 << n), "bin: ")
for i in range(num_qubits):
if theta & (1 << (num_qubits - i - 1)):
lam += Fraction(1, (1 << i))
qc.x(psi)
qc.h(q)
for i in range(num_qubits):
angle = (lam * (1 << i)) % 2
if angle > 1:
angle -= 2
if angle != 0:
qc.cp(angle * np.pi, psi, q[i])
qc.compose(
QFT(num_qubits=num_qubits, inverse=True),
inplace=True,
qubits=list(range(num_qubits)),
)
qc.barrier()
qc.measure(q, c)
return qc
def grover(num_qubits: int, ancillary_mode: str = "noancilla") -> QuantumCircuit:
"""Returns a quantum circuit implementing Grover's algorithm.
Keyword arguments:
num_qubits -- number of qubits of the returned quantum circuit
ancillary_mode -- defining the decomposition scheme
"""
num_qubits = num_qubits - 1 # -1 because of the flag qubit
q = QuantumRegister(num_qubits, "q")
flag = AncillaRegister(1, "flag")
state_preparation = QuantumCircuit(q, flag)
state_preparation.h(q)
state_preparation.x(flag)
oracle = QuantumCircuit(q, flag)
oracle.mcp(np.pi, q, flag)
operator = GroverOperator(oracle, mcx_mode=ancillary_mode)
iterations = Grover.optimal_num_iterations(1, num_qubits)
num_qubits = operator.num_qubits - 1 # -1 because last qubit is "flag" qubit and already taken care of
# num_qubits may differ now depending on the mcx_mode
q2 = QuantumRegister(num_qubits, "q")
qc = QuantumCircuit(q2, flag, name="grover")
qc.compose(state_preparation, inplace=True)
qc.compose(operator.power(iterations), inplace=True)
qc.measure_all()
qc.name = qc.name + "-" + ancillary_mode
return qc
def db_qasm_generator(algorithm_name, max_qubit):
directory = os.path.join('db_qasm_true')
if not os.path.exists(directory):
os.makedirs(directory)
circuit_func = None
# Patterns
if algorithm_name == "h_0":
circuit_func = h_0
elif algorithm_name == "h_c":
circuit_func = h_c
elif algorithm_name == "gen_ghz":
circuit_func = gen_ghz
elif algorithm_name == "rx_c":
circuit_func = rx_c
elif algorithm_name == "rx_gradually_c":
circuit_func = rx_gradually_c
# Algorithms
elif algorithm_name == "qft":
circuit_func = qft
elif algorithm_name == "qpe":
circuit_func = qpe
elif algorithm_name == "grover":
circuit_func = grover
# Others
else:
print("Unsupported algorithm.")
return
for n in range(2, max_qubit + 1):
circuit = circuit_func(n)
qasm_str = qasm2.dumps(circuit)
filename = os.path.join(directory, f"{algorithm_name}_q{n}.qasm")
with open(filename, "w") as file:
file.write(qasm_str)
print(f"Saved {filename}")
if __name__ == "__main__":
# db_qasm_generator('h_0', 40)
# db_qasm_generator('h_c', 40)
db_qasm_generator('gen_ghz', 40)
# db_qasm_generator('rx_c', 40)
# db_qasm_generator('rx_gradually_c', 40)
# db_qasm_generator('qft', 20)
# db_qasm_generator('qpe', 20)
# db_qasm_generator('grover', 15)