Skip to content

Commit

Permalink
Support basic single qubit controlled gates (#89)
Browse files Browse the repository at this point in the history
* Support basic single qubit controlled gates
- The single controlled gate sets are supported but requires a slight modification
- The multicontrolled gates can be considered in future versions

* Lint fixes

* Remove string based comparision and move to dict

* Add test cases

* Lint: Single qubit gates

* Update tests/pyquil_convert_test.py

---------

Co-authored-by: cqc-melf <[email protected]>
  • Loading branch information
glanzz and cqc-melf authored Aug 5, 2024
1 parent c1ec057 commit 011083e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
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

0 comments on commit 011083e

Please sign in to comment.