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 basic single qubit controlled gates #89

Merged
merged 10 commits into from
Aug 5, 2024
Merged
13 changes: 12 additions & 1 deletion pytket/extensions/pyquil/pyquil_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,16 @@
"PHASE": OpType.U1,
"SWAP": OpType.SWAP,
"XY": OpType.ISWAP,
"CH": OpType.CH,
"CY": OpType.CY,
}


_known_quil_gate_rev = {v: k for k, v in _known_quil_gate.items()}

# Gates with single controlled operation
_single_control_gates = {"CH": "H", "CY": "Y"}


def param_to_pyquil(p: Union[float, Expr]) -> Union[float, Expression]:
ppi = p * pi
Expand Down Expand Up @@ -302,7 +307,13 @@ def tk_to_pyquil(
"Cannot convert tket Op to pyQuil gate: " + op.get_name()
) from error
params = [param_to_pyquil(p) for p in op.params]
g = Gate(gatetype, params, qubits)
if gatetype in _single_control_gates:
g = Gate(_single_control_gates[gatetype], params, [qubits[1]]).controlled(
qubits[0]
)
else:
g = Gate(gatetype, params, qubits)

p += g
for m in measures:
p += m
Expand Down
23 changes: 23 additions & 0 deletions tests/pyquil_convert_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,29 @@ def test_from_tket() -> None:
) # 5 gates, 2 measures, and an initial declaration of classical register


def test_conversion_of_controlled_y() -> None:
single_controlled_gates_attributes = [
{"name": "Y", "qubits": [0, 1]},
{"name": "H", "qubits": [1, 0]},
]
c = Circuit(2, 2)
c.CY(*single_controlled_gates_attributes[0]["qubits"])
c.CH(*single_controlled_gates_attributes[1]["qubits"])

p = tk_to_pyquil(c)

for i, attributes in enumerate(single_controlled_gates_attributes):
Instruction = p.instructions[i + 1]
for attribute in attributes:
expected_attribute_value = attributes[attribute]
value = getattr(Instruction, attribute)
if attribute == "qubits":
for i, expected_qubit_index in enumerate(expected_attribute_value):
assert value[i].index == expected_qubit_index
else:
assert value == expected_attribute_value


def test_measure() -> None:
p = get_test_program(True)
m_map = {}
Expand Down