-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqvector.py
More file actions
147 lines (109 loc) · 4.83 KB
/
qvector.py
File metadata and controls
147 lines (109 loc) · 4.83 KB
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
from qiskit import IBMQ
import numpy as np
from qiskit.quantum_info import random_unitary
from optimizer import LBFGSOptimizer
from qc_runner import CustomNoiseModelQcRunner
from utils import get_classical_register, get_quantum_register, get_new_qc, get_I, pairwise, bold
IBMQ.load_account()
NUMBER_OF_CYCLES_FOR_AVERAGE_FIDELITY_CALCULATION = 10
VERBOSE = False
def get_variational_parametrized_qc(qubits, params):
register = get_quantum_register(len(qubits))
qc = get_new_qc(register)
i = 0
parameters_are_exhausted = False
while not parameters_are_exhausted:
for qubit in qubits:
# rotate qubit with an angle from parameters
theta = params[i] * 2 * np.pi
qc.rz(theta, qubit)
i += 1
for qubit in qubits:
# rotate qubit with an angle from parameters
theta = params[i] * 2 * np.pi
qc.rx(theta, qubit)
i += 1
for qubit_pair in pairwise(qubits):
# do control-Z rotations for neighbour qubits using angles from the params array
theta = params[i] * 2 * np.pi
qc.crz(theta, *list(qubit_pair))
i += 1
parameters_are_exhausted = i < len(params)
return qc
def get_encode_qc(qubits, params):
# on n qubits. k logical and n-k syndrome qubits
return get_variational_parametrized_qc(qubits, params)
def get_recovery_qc(qubits, params):
# on n + r qubits. k logical and n-k syndrome qubits, r is the number of refresh qubits
# my guess is that this is the same as above?
return get_variational_parametrized_qc(qubits, params)
def get_sample_state_qc(k):
# k is the number of available logical qubits
return random_unitary(2 ** k).to_instruction()
def estimate_average_fidelity(p, q, k, n, r, qc_runner, number_of_iterations=1):
if VERBOSE:
print("Running optimization with params p and q:")
print("p =", p)
print("q =", q)
fidelities = []
# We calculate the fidelity for L different S samples
for i in range(NUMBER_OF_CYCLES_FOR_AVERAGE_FIDELITY_CALCULATION):
qc = get_new_qc(get_quantum_register(n + r), get_classical_register(n + r))
# sample the state S
init_qc = get_sample_state_qc(k)
encode_qc = get_encode_qc(range(n), p)
recovery_qc = get_recovery_qc(range(n + r), q)
noisy_I = get_I(n)
init_reg = range(k)
encode_reg = range(n)
recover_reg = range(n + r)
noise_reg = range(n)
# init the state S
# apply noisy encoding(q)
# apply recovery(p)
# apply (noisy encoding)^(dagger) = decoding(q)
# apply (S)^(dagger)
qc.append(init_qc, init_reg)
qc.append(encode_qc, encode_reg)
for i in range(number_of_iterations):
qc.unitary(noisy_I, noise_reg, label='noisy_I')
qc.append(recovery_qc, recover_reg)
qc.append(encode_qc.inverse(), encode_reg)
qc.append(init_qc.inverse(), init_reg)
# run the circuit and get results
# the refresh qubits should work without noise?
fidelity = qc_runner.get_fidelity(qc)
fidelities.append(fidelity)
# estimate the average fidelity for q and p
average_fidelity = sum(fidelities)/len(fidelities)
if VERBOSE:
print(bold(f"For current p and q average fidelity was: {average_fidelity}"))
print("=========================================")
print()
return average_fidelity
def get_params_length(k, n, r):
total_qubits = n + r
params_per_cycle = int(total_qubits * 3) * 2 # x2 for p and q
return params_per_cycle
def optimize(k, n, r, qc_runner, optimizer=LBFGSOptimizer(), initial_params=None, number_of_cycles=2):
# k is the number of logical qubits, n-k is the number of syndrom qubits and r is the number of refresh qubits
def init_params():
params_per_cycle = get_params_length(k, n, r)
return np.random.rand(number_of_cycles * params_per_cycle)
def fn(x, args):
p, q = x[:len(x)//2], x[len(x)//2:]
return estimate_average_fidelity(p, q, *args)
print("Optimization started")
if initial_params is None:
initial_params = init_params()
return optimizer.optimize(fn, initial_params, [k, n, r, qc_runner])
if __name__ == "__main__":
k, n, r = 1, 5, 3
# k, n, r = 1, 3, 1
cycles = 1
params_len = get_params_length(k, n, r) * cycles
f = estimate_average_fidelity(np.random.rand(params_len//2), np.random.rand(params_len//2), k, n, r,
qc_runner=CustomNoiseModelQcRunner(n, r))
print("Fidelity is: ", f)
res = optimize(k, n, r, qc_runner=CustomNoiseModelQcRunner(n, r), optimizer=LBFGSOptimizer())
print("Optimization results: ", res)