-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Qudit phase gate, Z_d #194
Changes from 5 commits
671ba7c
2d08087
bb6b5e2
329bcc4
d927d93
eaac6bd
6a0fb11
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -252,6 +252,88 @@ def test_iswap(q0: int, q1: int): | |
assert np.all(results.measurements["m1"] == q0) | ||
|
||
|
||
@pytest.mark.parametrize("dimension, phase_rads", [(2, np.pi), (3, 1), (4, np.pi * 2)]) | ||
def test_rz_unitary(dimension: float, phase_rads: float): | ||
rz = qudit_gates.QuditRzGate(dimension=dimension, radians=phase_rads) | ||
expected_unitary = np.identity(n=dimension, dtype=np.complex64) | ||
|
||
# 1j = e ^ ( j * ( pi / 2 )), so we multiply phase_rads by 2 / pi. | ||
expected_unitary[dimension - 1][dimension - 1] = 1j ** (phase_rads * 2 / np.pi) | ||
|
||
assert np.isclose(phase_rads / np.pi, rz._exponent) | ||
rz_unitary = cirq.unitary(rz) | ||
assert np.allclose(cirq.unitary(rz), expected_unitary) | ||
assert np.allclose(np.eye(len(rz_unitary)), rz_unitary.dot(rz_unitary.T.conj())) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd feel more comfortable if we had a test on the action of the QuditRzGate too. Can we test the qudit equivalent of HZH and make sure it is equivalent to X? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe let's make this a follow-up PR until the qudit Hadamard PR issue gets resolved. |
||
|
||
|
||
@pytest.mark.parametrize( | ||
"phase_1, phase_2, addend, expected_state", | ||
[ | ||
(0, 0, 1, 2), | ||
(np.pi * 2 / 3, np.pi * 4 / 3, 0, 2), | ||
(np.pi * 4 / 3, np.pi * 2 / 3, 0, 1), | ||
], | ||
) | ||
def test_X_HZH_qudit_identity( | ||
phase_1: float, phase_2: float, addend: int, expected_state: int | ||
): | ||
# For d=3, there are three identities: one for each swap. | ||
# HH is equivalent to swapping |1> with |2> | ||
# Applying a 1/3 turn to |1> and a 2/3 turn to |2> results in swapping | ||
# |0> and |2> | ||
# Applying a 2/3 turn to |1> and a 1/3 turn to |2> results in swapping | ||
# |0> and |1> | ||
qutrit = cirq.NamedQid("q0", dimension=3) | ||
c = cirq.Circuit() | ||
c.append(qudit_gates.QuditPlusGate(3, addend=addend)(qutrit)) | ||
c.append(qudit_gates.QuditHadamardGate(dimension=3)(qutrit)) | ||
c.append( | ||
qudit_gates.QuditRzGate(dimension=3, radians=phase_1, phased_state=1)(qutrit) | ||
) | ||
c.append( | ||
qudit_gates.QuditRzGate(dimension=3, radians=phase_2, phased_state=2)(qutrit) | ||
) | ||
c.append(qudit_gates.QuditHadamardGate(dimension=3)(qutrit)) | ||
c.append(cirq.measure(qutrit, key="m")) | ||
sim = cirq.Simulator() | ||
results = sim.run(c, repetitions=1000) | ||
assert np.all(results.measurements["m"] == expected_state) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"phase_1, phase_2, addend, expected_state", | ||
[ | ||
(0, 0, 1, 2), | ||
(np.pi * 2 / 3, np.pi * 4 / 3, 0, 2), | ||
(np.pi * 4 / 3, np.pi * 2 / 3, 0, 1), | ||
], | ||
) | ||
def test_X_HZH_qudit_identity( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this function repeated twice? |
||
phase_1: float, phase_2: float, addend: int, expected_state: int | ||
): | ||
# For d=3, there are three identities: one for each swap. | ||
# HH is equivalent to swapping |1> with |2> | ||
# Applying a 1/3 turn to |1> and a 2/3 turn to |2> results in swapping | ||
# |0> and |2> | ||
# Applying a 2/3 turn to |1> and a 1/3 turn to |2> results in swapping | ||
# |0> and |1> | ||
qutrit = cirq.NamedQid("q0", dimension=3) | ||
c = cirq.Circuit() | ||
c.append(qudit_gates.QuditPlusGate(3, addend=addend)(qutrit)) | ||
c.append(qudit_gates.QuditHadamardGate(dimension=3)(qutrit)) | ||
c.append( | ||
qudit_gates.QuditRzGate(dimension=3, radians=phase_1, phased_state=1)(qutrit) | ||
) | ||
c.append( | ||
qudit_gates.QuditRzGate(dimension=3, radians=phase_2, phased_state=2)(qutrit) | ||
) | ||
c.append(qudit_gates.QuditHadamardGate(dimension=3)(qutrit)) | ||
c.append(cirq.measure(qutrit, key="m")) | ||
sim = cirq.Simulator() | ||
results = sim.run(c, repetitions=1000) | ||
assert np.all(results.measurements["m"] == expected_state) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"q0, q1", [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)] | ||
) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: would probably change the second colon to a period (then capitalize "For")