Skip to content
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

Merged
merged 5 commits into from
Nov 3, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions pennylane_cirq/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,11 @@ class CPhase(Operation):

CPhase(\phi) =
\begin{bmatrix}
[[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, e^{i\phi]]
\end{bmatrix}.
1 & 0 & 0 & 0\\
0 & 1 & 0 & 0\\
0 & 0 & 1 & 0\\
0 & 0 & 0 & e^{i\phi}
\end{bmatrix}

See the `Cirq docs <https://cirq.readthedocs.io/en/stable/generated/cirq.CZPowGate.html>`_
for further details."""
Expand Down
28 changes: 16 additions & 12 deletions tests/test_native_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,22 +191,24 @@ def test_apply_amplitude_damp_single_wire(

@pytest.mark.parametrize("input",
[
[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1],
np.array([1, 0, 0, 0]),
np.array([2, 1, 0, 1]) / np.sqrt(6),
np.array([0, 0, 1, 0]),
np.array([0, 1, 0, 1]) / np.sqrt(2),
np.array([0, 0, 0, 1]),
np.array([2, 1, 2, 1]) / np.sqrt(10),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

]
)
def test_apply_iswap(self, tol, input, shots, analytic):
"""Tests that applying the CPhase gate yields the expected output."""
"""Tests that applying the iSWAP gate yields the expected output."""
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
expected = iswap_mat @ input

device.reset()
device._initial_state = np.array(input, dtype=np.complex64)
Expand All @@ -217,22 +219,24 @@ def test_apply_iswap(self, tol, input, shots, analytic):
@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],
np.array([1, 0, 0, 0]),
np.array([2, 1, 0, 1]) / np.sqrt(6),
np.array([0, 0, 1, 0]),
np.array([0, 1, 0, 1]) / np.sqrt(2),
np.array([0, 0, 0, 1]),
np.array([2, 1, 2, 1]) / np.sqrt(10),
]
)
def test_apply_cphase(self, tol, par, input, shots, analytic):
"""Tests that applying the iSWAP gate yields the expected output."""
"""Tests that applying the CPhase 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
expected = cphase_mat @ input

device.reset()
device._initial_state = np.array(input, dtype=np.complex64)
Expand Down