Unverified Commit f5f0a7a4 authored by dwuggh's avatar dwuggh
Browse files

fix bug in QOperator.channel

parent 66284986
......@@ -91,6 +91,8 @@ class DensityOperator(QOperator):
ρ_10 = multiply(self, P_10).partial_trace([q1, q2])
ρ_11 = multiply(self, P_11).partial_trace([q1, q2])
print()
p_00 = ρ_00.operator.trace()
p_01 = ρ_01.operator.trace()
......
......@@ -17,7 +17,6 @@ class QChannel(object):
self.qubits.append(q)
self.qubits = np.array(self.qubits, dtype=np.int32)
self.qmax = np.max(self.qubits) + 1
# broadcast every operator
# for op in self.kraus_operators:
# op = op.broadcast(qnum)
......@@ -44,7 +43,7 @@ def depolarizing_channel(p, qubits = [0]) -> QChannel:
continue
indices = np.flip(get_n_digits(i, 4, qnum))
# qi: a tuple, (q, i)
ps = map(lambda qi: pauli(qi[1], qi[0]), list(enumerate(indices)))
ps = map(lambda qi: pauli(qi[1], qubits[qi[0]]), list(enumerate(indices)))
op = reduce(multiply, ps)
op.operator *= np.sqrt(p / (4 ** qnum - 1))
operators.append(op)
......
......@@ -14,9 +14,6 @@ class QOperator(object):
def qnum(self):
return self.qubits.size
def qmax(self):
return np.max(self.qubits) + 1
def dim(self):
# the following should be equivalent:
# return 2 ** self.qubits.size
......@@ -37,31 +34,6 @@ class QOperator(object):
operator = self.operator * scalar
return QOperator(self.qubits, operator)
'''
construct gate in the big hilbert space
G = I ⊗ G
'''
def broadcast(self, qnum: int):
dim = 2 ** qnum
big_operator = np.zeros((dim, dim), dtype=np.float64)
for i in range(dim):
for j in range(dim):
di = np.flip(get_bin_digits(i, qnum))
dj = np.flip(get_bin_digits(j, qnum))
# coordinates in the small Hilbert space
gi = from_bin_digits(np.flip(di[self.qubits]))
gj = from_bin_digits(np.flip(dj[self.qubits]))
# check for identity
id = 1
for k in range(len(di)):
if id == 1 and not contains(self.qubits, k) and di[k] != dj[k]:
id = 0
val = np.float64(id) * self.operator[gi, gj]
big_operator[i, j] = val
return big_operator
'''
construct gate in the big hilbert space, with given qubits indices
......@@ -111,7 +83,7 @@ class QOperator(object):
dim = self.dim()
qnum = self.qnum()
qubits_reserved = np.fromiter((x for x in self.qubits if not contains(traced_qubits, x)), np.int32)
qubits_reserved = np.fromiter(filter(lambda x: not contains(traced_qubits, x), self.qubits), np.int32)
dim_new = 2 ** len(qubits_reserved)
......
......@@ -39,6 +39,24 @@ GHZ = [
[0, 0.0003899, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.004329, 0],
[ 0.4406, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.4864],
]
GHZ = [
[0.4879, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.4486],
[0, 0.001544, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.0003475, 0],
[0, 0, 0.001544, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.0003475, 0, 0],
[0, 0, 0, 0.0002442, 0, 0, 0, 0, 0, 0, 0, 0, 8.566e-05, 0, 0, 0],
[0, 0, 0, 0, 0.002165, 0, 0, 0, 0, 0, 0, 0.0007082, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0.004415, 0, 0, 0, 0, 0.002873, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1.149e-05, 0, 0, 5.486e-07, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0.002165, 0.0007082, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0.0007082, 0.002165, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 5.486e-07, 0, 0, 1.149e-05, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0.002873, 0, 0, 0, 0, 0.004415, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0.0007082, 0, 0, 0, 0, 0, 0, 0.002165, 0, 0, 0, 0],
[0, 0, 0, 8.566e-05, 0, 0, 0, 0, 0, 0, 0, 0, 0.0002442, 0, 0, 0],
[0, 0, 0.0003475, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.001544, 0, 0],
[0, 0.0003475, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.001544, 0],
[0.4486, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.4879],
]
GHZ = np.array(GHZ)
dim = GHZ.shape[0]
......@@ -56,7 +74,7 @@ ps = [p_even, p_odd]
p = ps[0]
# p = 0.5
# ps = [p, 1 - p]
print(p_even, p_odd)
print("p_even, p_odd:", p_even, p_odd)
def get_p(indices, p_g):
......@@ -101,10 +119,10 @@ def get_final_result(err_model: cc.ErrorModel, p):
# c_error[dsi] += p_error
# channel for GHZ qubit
# X error does not matter, Z error can flip measurement outcome
if i == 0:
print(pg_correct, pg_error)
print(p_correct, p_error)
print(dsi)
# if i == 0:
# print(pg_correct, pg_error)
# print(p_correct, p_error)
# print(dsi)
# print(c_correct[dsi])
......@@ -118,6 +136,7 @@ def measure_GHZ(p_m):
pm_error = 4 * p_m * (1 - p_m) ** 3 + 4 * p_m ** 3 * (1 - p_m)
return pm_correct, pm_error
# p = 0.9535
c_correct, c_error = get_final_result(cc.ErrorModel(0.1, 0.006, 0.006), p)
print(c_correct[0, 0, 0])
print(np.sum(c_correct), np.sum(c_error))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment