some fixes to krisi's project

This commit is contained in:
Daniel Tsvetkov 2020-03-29 20:38:08 +02:00
parent 6ae883d818
commit 908efa9b76
2 changed files with 19 additions and 15 deletions

View File

@ -58,7 +58,7 @@ def test_krisi_measurement_2():
def test_krisi_measurement_3():
iterations = 400
beta = np.pi * 0.62
beta = np.pi + 0.62
CASE_ID_ID = "id_id"
CASE_ID_ORT = "id_ort"
@ -67,14 +67,14 @@ def test_krisi_measurement_3():
case_choices = [CASE_ID_ID, CASE_ID_ORT, CASE_ORT_ID, CASE_ORT_ORT]
b_0 = State(1 / np.sqrt(2) * (s("|100>") - s("|010>")))
b_1 = State(1 / np.sqrt(2) * (s("|011>") - s("|101>")))
b_0 = State((1 / np.sqrt(2)) * (s("|100>") + s("|010>")))
b_1 = State((1 / np.sqrt(2)) * (s("|011>") + s("|101>")))
basis = [
s("|000>"),
State(np.cos(beta) * b_0 + np.sin(beta) * s("|001>")),
State(-np.sin(beta) * b_0 + np.cos(beta) * s("|001>")),
b_0,
b_1,
State((1 / np.sqrt(2)) * (s("|100>") - s("|010>"))),
State((1 / np.sqrt(2)) * (s("|011>") - s("|101>"))),
State(np.cos(beta) * b_1 + np.sin(beta) * s("|001>")),
State(-np.sin(beta) * b_1 + np.cos(beta) * s("|001>")),
s("|111>"),
@ -107,16 +107,18 @@ def test_krisi_measurement_3():
q2 = State.from_bloch_angles(theta1, phi1)
q1q2_st = State(q1*q2)
meas1 = q1q2_st.measure(basis=bell_basis)
if meas1 == '11':
return 'discarded'
# meas1 = q1q2_st.measure(basis=bell_basis)
# if meas1 == '11':
# return 'discarded'
# TODO:
# P_minus = b_psi_m.x(b_psi_m)
P_minus = b_psi_m.x(b_psi_m)
P_s = b_psi_p.x(b_psi_p) + b_phi_m.x(b_phi_m) + b_phi_p.x(b_phi_p)
meas = MeasurementOperator(P_s.m).on(q1q2_st)
post_state = State(State.normalize(meas))
# vec = MeasurementOperator(P_minus.m).on(q1q2_st)
# if np.all(vec.m.transpose() == np.zeros(4)):
# return 'identical'
vec2 = MeasurementOperator(P_s.m).on(q1q2_st)
post_state = State(State.normalize(vec2))
if case == CASE_ID_ID:
assert post_state == q1q2_st
@ -124,7 +126,7 @@ def test_krisi_measurement_3():
assert post_state != q1q2_st
q3 = State.from_bloch_angles(theta2, phi2)
meas = State(State.normalize(post_state * q3)).measure(basis=basis)
meas = State(post_state * q3).measure(basis=basis)
# # TODO: This part measures the first two qubits in Bell basis, then:
# # 1. constructs a new pure state

6
lib.py
View File

@ -353,8 +353,10 @@ class State(Vector):
else:
# TODO: This may be wrong
# normalize the weights
weights = list(np.array(weights) / sum(weights))
assert np.isclose(sum(weights), 1.0)
weights_sum = np.sum(weights)
if not np.isclose(weights_sum, 1.0):
weights = list(np.array(weights) / weights_sum)
assert np.isclose(np.sum(weights), 1.0)
format_str = self.get_fmt_of_element()
choices = empty_choices + [format_str.format(i) for i in