-
Notifications
You must be signed in to change notification settings - Fork 18
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 iSWAP and CPhase gates #45
Changes from 3 commits
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 |
---|---|---|
|
@@ -19,7 +19,7 @@ | |
|
||
import pennylane as qml | ||
import numpy as np | ||
from pennylane_cirq import ops, MixedStateSimulatorDevice | ||
from pennylane_cirq import ops, MixedStateSimulatorDevice, SimulatorDevice | ||
import cirq | ||
|
||
|
||
|
@@ -188,3 +188,54 @@ def test_apply_amplitude_damp_single_wire( | |
simulator_device_1_wire.apply([ops.AmplitudeDamp(*par, wires=[0])]) | ||
|
||
assert np.allclose(simulator_device_1_wire.state, expected_density_matrix, **tol) | ||
|
||
@pytest.mark.parametrize("input", | ||
[ | ||
[1, 0, 0, 0], | ||
[0, 1, 0, 0], | ||
[0, 0, 1, 0], | ||
[0, 0, 0, 1], | ||
] | ||
) | ||
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 struggled to understand this. If the device has two wires, then shouldn't the underlying pure state have shape 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. It should have shape 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. Aha, got it! the pytest parametrize format looks exactly like a 2d array 😆 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. Might be good to test on superposition states as well |
||
def test_apply_iswap(self, tol, input, shots, analytic): | ||
"""Tests that applying the CPhase gate yields the expected output.""" | ||
thisac marked this conversation as resolved.
Show resolved
Hide resolved
|
||
device = SimulatorDevice(2, shots=shots, analytic=analytic) | ||
|
||
iswap_mat = np.array([[1, 0, 0, 0], | ||
[0, 0, 1j, 0], | ||
[0, 1j, 0, 0], | ||
[0, 0, 0, 1]]) | ||
|
||
expected = input @ iswap_mat | ||
thisac marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
device.reset() | ||
device._initial_state = np.array(input, dtype=np.complex64) | ||
device.apply([ops.ISWAP(wires=[0, 1])]) | ||
|
||
assert np.allclose(device.state, expected, **tol) | ||
|
||
@pytest.mark.parametrize("par", [0, 0.5, 1.42, np.pi/4, np.pi/2, np.pi]) | ||
@pytest.mark.parametrize("input", | ||
[ | ||
[1, 0, 0, 0], | ||
[0, 1, 0, 0], | ||
[0, 0, 1, 0], | ||
[0, 0, 0, 1], | ||
] | ||
) | ||
def test_apply_cphase(self, tol, par, input, shots, analytic): | ||
"""Tests that applying the iSWAP gate yields the expected output.""" | ||
device = SimulatorDevice(2, shots=shots, analytic=analytic) | ||
|
||
cphase_mat = np.array([[1, 0, 0, 0], | ||
[0, 1, 0, 0], | ||
[0, 0, 1, 0], | ||
[0, 0, 0, np.exp(1j * par)]]) | ||
|
||
expected = input @ cphase_mat | ||
|
||
device.reset() | ||
device._initial_state = np.array(input, dtype=np.complex64) | ||
device.apply([ops.CPhase(par, wires=[0, 1])]) | ||
|
||
assert np.allclose(device.state, expected, **tol) |
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.
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.
Doesn't the
bmatrix
need newlines?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.
You're completely right. This is a very ugly mix of Python and Latex. 😆