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

Support Controlled gates properly on PyQVM #1338

Open
wants to merge 3 commits into
base: rc
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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: 10 additions & 0 deletions pyquil/quilbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,16 @@ def __init__(
self.qubits = qubits_list
self.modifiers: List[str] = []

@property
def modified_name(self) -> str:
""" If there's a modifier on this gate then the 'official' name
of the gate needs to change, otherwise gate lookups for
matrices don't work.
"""
if "CONTROLLED" in self.modifiers:
return "C" + self.name
return self.name

def get_qubits(self, indices: bool = True) -> Set[QubitDesignator]:
return {_extract_qubit_index(q, indices) for q in self.qubits}

Expand Down
4 changes: 2 additions & 2 deletions pyquil/simulation/_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,9 @@ def _get_gate_tensor_and_qubits(gate: Gate) -> Tuple[np.ndarray, List[int]]:
:return: tensor, qubit_inds.
"""
if len(gate.params) > 0:
matrix = QUANTUM_GATES[gate.name](*gate.params)
matrix = QUANTUM_GATES[gate.modified_name](*gate.params)
else:
matrix = QUANTUM_GATES[gate.name]
matrix = QUANTUM_GATES[gate.modified_name]

qubit_inds = [q.index for q in gate.qubits]

Expand Down
15 changes: 14 additions & 1 deletion pyquil/simulation/matrices.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@

import numpy as np

INV_ROOT2 = 1.0 / np.sqrt(2.0)

I = np.array([[1.0, 0.0], [0.0, 1.0]])

X = np.array([[0.0, 1.0], [1.0, 0.0]])
Expand All @@ -115,7 +117,16 @@

Z = np.array([[1.0, 0.0], [0.0, -1.0]])

H = (1.0 / np.sqrt(2.0)) * np.array([[1.0, 1.0], [1.0, -1.0]])
H = INV_ROOT2 * np.array([[1.0, 1.0], [1.0, -1.0]])

CH = np.array(
[
[1.0, 0.0, 0.0, 0.0],
[0.0, 1.0, 0.0, 0.0],
[0.0, 0.0, INV_ROOT2, INV_ROOT2],
[0.0, 0.0, INV_ROOT2, -INV_ROOT2],
]
)

S = np.array([[1.0, 0.0], [0.0, 1.0j]])

Expand Down Expand Up @@ -236,9 +247,11 @@ def BARENCO(alpha: float, phi: float, theta: float) -> np.ndarray:
QUANTUM_GATES = {
"I": I,
"X": X,
"CX": CNOT,
"Y": Y,
"Z": Z,
"H": H,
"CH": CH,
"S": S,
"T": T,
"PHASE": PHASE,
Expand Down
30 changes: 30 additions & 0 deletions pyquil/tests/test_pyqvm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import pytest

from pyquil import Program
from pyquil.gates import H, X, CNOT, Z, SWAP
from pyquil.pyqvm import PyQVM


@pytest.mark.parametrize(
"first_gate,second_gate,expected",
[
(X(0), H(1), "X 0\nH 1\n"),
(X(0), H(1).controlled(0), "X 0\nCONTROLLED H 0 1\n"),
(H(0), X(1), "H 0\nX 1\n"),
(H(0), X(1).controlled(0), "H 0\nCONTROLLED X 0 1\n"),
(H(0), Z(1), "H 0\nZ 1\n"),
(H(0), Z(1).controlled(0), "H 0\nCONTROLLED Z 0 1\n"),
(X(0), X(1), "X 0\nX 1\n"),
(X(0), X(1).controlled(0), "X 0\nCONTROLLED X 0 1\n"),
(H(0), SWAP(0, 1), "H 0\nSWAP 0 1\n"),
(X(0), CNOT(0, 1), "X 0\nCNOT 0 1\n"),
],
)
def test_pyqvm_controlled_gates(first_gate, second_gate, expected):
""" Unit-test based on the bug report in
https://github.com/rigetti/pyquil/issues/1259
"""
p = Program(first_gate, second_gate)
qvm = PyQVM(n_qubits=2)
result = qvm.execute(p)
assert result.program.out() == expected