Skip to content

Commit

Permalink
Support for additional IonQ gates (#212)
Browse files Browse the repository at this point in the history
* added support for more gates, now all gates are multi-control and multi-target
  • Loading branch information
JamesB-1qbit authored Sep 22, 2022
1 parent 3ce7079 commit 76f4123
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 39 deletions.
27 changes: 6 additions & 21 deletions tangelo/linq/tests/test_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,31 +382,16 @@ def test_json_ionq(self):
abs_circ_ionq = Circuit(abs_gates)
json_ionq_circ = translator.translate_json_ionq(abs_circ_ionq)

ref_circuit = {'circuit': [{'gate': 'x', 'target': 0},
{'gate': 'x', 'target': 1},
{'gate': 'rx', 'rotation': 1.5707963267948966, 'target': 0},
{'gate': 'h', 'target': 2},
{'control': 0, 'gate': 'cnot', 'target': 1},
{'gate': 'rz', 'rotation': 12.566170614359173, 'target': 2}],
ref_circuit = {'circuit': [{'gate': 'x', 'targets': [0]},
{'gate': 'x', 'targets': [1]},
{'gate': 'rx', 'targets': [0], 'rotation': 1.5707963267948966},
{'gate': 'h', 'targets': [2]},
{'gate': 'x', 'targets': [1], 'controls': [0]},
{'gate': 'rz', 'targets': [2], 'rotation': 12.566170614359173}],
'qubits': 3}

assert(json_ionq_circ == ref_circuit)

def test_translate_ionq_inverse(self):
""" Test that inverse of T and S circuits for ionQ return Tdag and Sdag after translation """

# Generate [Gate("Tdag", 0), Gate("Sdag", 0)] equivalent, and its hardcoded inverse
circ = Circuit([Gate("PHASE", 0, parameter=-np.pi/4), Gate("PHASE", 0, parameter=-np.pi/2)])
inverse_circ = Circuit([Gate("S", 0), Gate("T", 0)])

ionq_circ_inverse = translator.translate_json_ionq(circ.inverse())
ionq_inverse_circ = translator.translate_json_ionq(inverse_circ)
ionq_circ = translator.translate_json_ionq(circ)

ionq_ref = {'qubits': 1, 'circuit': [{'gate': 'ti', 'target': 0}, {'gate': 'si', 'target': 0}]}
self.assertTrue(ionq_inverse_circ == ionq_circ_inverse)
self.assertTrue(ionq_circ == ionq_ref)

@unittest.skipIf("braket" not in installed_backends, "Test Skipped: Backend not available \n")
def test_braket(self):
"""
Expand Down
34 changes: 16 additions & 18 deletions tangelo/linq/translator/translate_json_ionq.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,15 @@ def get_ionq_gates():
"""

GATE_JSON_IONQ = dict()
for name in {"H", "X", "Y", "Z", "S", "T", "RX", "RY", "RZ", "CNOT", "PHASE"}:
for name in {"H", "X", "Y", "Z", "S", "T", "RX", "RY", "RZ", "PHASE", "SWAP", "XX"}:
GATE_JSON_IONQ[name] = name.lower()
for name in {"CRX", "CRY", "CRZ"}:
GATE_JSON_IONQ[name] = name[1:].lower()
for name in {"CX", "CY", "CZ"}:
GATE_JSON_IONQ[name] = name[1:].lower()
GATE_JSON_IONQ["CNOT"] = "x"
GATE_JSON_IONQ["PHASE"] = "z"
GATE_JSON_IONQ["CPHASE"] = "z"
return GATE_JSON_IONQ


Expand All @@ -54,23 +61,14 @@ def translate_json_ionq(source_circuit):

json_gates = []
for gate in source_circuit._gates:
if gate.name in {"H", "X", "Y", "Z", "S", "T"}:
json_gates.append({'gate': GATE_JSON_IONQ[gate.name], 'target': gate.target[0]})
elif gate.name in {"RX", "RY", "RZ"}:
json_gates.append({'gate': GATE_JSON_IONQ[gate.name], 'target': gate.target[0], 'rotation': gate.parameter})
elif gate.name in {"PHASE"}:
if isclose(gate.parameter, pi / 2, abs_tol=1.e-7):
json_gates.append({'gate': 's', 'target': gate.target[0]})
elif isclose(gate.parameter, - pi / 2, abs_tol=1.e-7):
json_gates.append({'gate': 'si', 'target': gate.target[0]})
elif isclose(gate.parameter, pi / 4, abs_tol=1.e-7):
json_gates.append({'gate': 't', 'target': gate.target[0]})
elif isclose(gate.parameter, - pi / 4, abs_tol=1.e-7):
json_gates.append({'gate': 'ti', 'target': gate.target[0]})
else:
raise ValueError(f"Only phases of pi/2, -pi/2, pi/4, -pi/4 are supported with JSON IonQ translation")
elif gate.name in {"CNOT"}:
json_gates.append({'gate': GATE_JSON_IONQ[gate.name], 'target': gate.target[0], 'control': gate.control[0]})
if gate.name in {"H", "X", "Y", "Z", "S", "T", "SWAP"}:
json_gates.append({'gate': GATE_JSON_IONQ[gate.name], 'targets': gate.target})
elif gate.name in {"RX", "RY", "RZ", "PHASE", "XX"}:
json_gates.append({'gate': GATE_JSON_IONQ[gate.name], 'targets': gate.target, 'rotation': gate.parameter})
elif gate.name in {"CRX", "CRY", "CRZ", "CPHASE"}:
json_gates.append({'gate': GATE_JSON_IONQ[gate.name], 'targets': gate.target, 'controls': gate.control, 'rotation': gate.parameter})
elif gate.name in {"CX", "CY", "CZ", "CNOT"}:
json_gates.append({'gate': GATE_JSON_IONQ[gate.name], 'targets': gate.target, 'controls': gate.control})
else:
raise ValueError(f"Gate '{gate.name}' not supported with JSON IonQ translation")

Expand Down

0 comments on commit 76f4123

Please sign in to comment.