From 157d7ab2941dd71ac20d7533922f0f2ff19cba26 Mon Sep 17 00:00:00 2001 From: Cody Wang Date: Mon, 12 Feb 2024 19:32:58 -0800 Subject: [PATCH] Ignore global phase if unsupported (#156) --- qiskit_braket_provider/providers/adapter.py | 28 +++++++++++++++------ qiskit_braket_provider/version.py | 2 +- tests/providers/test_adapter.py | 17 +++++++++---- 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/qiskit_braket_provider/providers/adapter.py b/qiskit_braket_provider/providers/adapter.py index 8d00e22..a8f1014 100644 --- a/qiskit_braket_provider/providers/adapter.py +++ b/qiskit_braket_provider/providers/adapter.py @@ -32,6 +32,8 @@ from qiskit_braket_provider.exception import QiskitBraketException +_GPHASE_GATE_NAME = "global_phase" + _BRAKET_TO_QISKIT_NAMES = { "u": "u", "phaseshift": "p", @@ -63,6 +65,7 @@ "gpi": "gpi", "gpi2": "gpi2", "ms": "ms", + "gphase": _GPHASE_GATE_NAME, } _CONTROLLED_GATES_BY_QUBIT_COUNT = { @@ -108,12 +111,14 @@ "ecr": lambda: [braket_gates.ECR()], "iswap": lambda: [braket_gates.ISwap()], # IonQ gates - "gpi": lambda angle: [braket_gates.GPi(2 * pi * angle)], - "gpi2": lambda angle: [braket_gates.GPi2(2 * pi * angle)], - "ms": lambda angle_1, angle_2, angle_3: [ - braket_gates.MS(2 * pi * angle_1, 2 * pi * angle_2, 2 * pi * angle_3) + "gpi": lambda turns: [braket_gates.GPi(2 * pi * turns)], + "gpi2": lambda turns: [braket_gates.GPi2(2 * pi * turns)], + "ms": lambda turns_1, turns_2, turns_3: [ + braket_gates.MS(2 * pi * turns_1, 2 * pi * turns_2, 2 * pi * turns_3) ], "zz": lambda angle: [braket_gates.ZZ(2 * pi * angle)], + # Global phase + _GPHASE_GATE_NAME: lambda phase: [braket_gates.GPhase(phase)], } _QISKIT_CONTROLLED_GATE_NAMES_TO_BRAKET_GATES: dict[str, Callable] = { @@ -174,6 +179,7 @@ Parameter("phi1") / (2 * pi), Parameter("theta") / (2 * pi), ), + "gphase": qiskit_gates.GlobalPhaseGate(Parameter("theta")), } @@ -398,10 +404,11 @@ def to_braket( Returns: Circuit: Braket circuit """ - basis_gates = basis_gates or _TRANSLATABLE_QISKIT_GATE_NAMES if not isinstance(circuit, QuantumCircuit): raise TypeError(f"Expected a QuantumCircuit, got {type(circuit)} instead.") + basis_gates = set(basis_gates or _TRANSLATABLE_QISKIT_GATE_NAMES) + braket_circuit = Circuit() if not verbatim and not {gate.name for gate, _, _ in circuit.data}.issubset( basis_gates @@ -460,8 +467,15 @@ def to_braket( target=qubit_indices, ) - if circuit.global_phase > _EPS: - braket_circuit.gphase(circuit.global_phase) + global_phase = circuit.global_phase + if abs(global_phase) > _EPS: + if _GPHASE_GATE_NAME in basis_gates: + braket_circuit.gphase(global_phase) + else: + warnings.warn( + f"Device does not support global phase; " + f"global phase of {global_phase} will not be included in Braket circuit" + ) if verbatim: return Circuit(braket_circuit.result_types).add_verbatim_box( diff --git a/qiskit_braket_provider/version.py b/qiskit_braket_provider/version.py index 6d576ac..4a2ad4c 100644 --- a/qiskit_braket_provider/version.py +++ b/qiskit_braket_provider/version.py @@ -1,3 +1,3 @@ """Qiskit-Braket provider version.""" -__version__ = "0.1.0" +__version__ = "0.1.1" diff --git a/tests/providers/test_adapter.py b/tests/providers/test_adapter.py index c7ca026..06ede7f 100644 --- a/tests/providers/test_adapter.py +++ b/tests/providers/test_adapter.py @@ -9,7 +9,7 @@ from braket.devices import LocalSimulator from qiskit import ClassicalRegister, QuantumCircuit, QuantumRegister, transpile from qiskit.circuit import Parameter, ParameterVector -from qiskit.circuit.library import PauliEvolutionGate +from qiskit.circuit.library import GlobalPhaseGate, PauliEvolutionGate from qiskit.circuit.library import standard_gates as qiskit_gates from qiskit.providers.basicaer import BasicAer from qiskit.quantum_info import SparsePauliOp @@ -242,14 +242,20 @@ def test_global_phase(self): """Tests conversion when transpiler generates a global phase""" qiskit_circuit = QuantumCircuit(1, global_phase=np.pi / 2) qiskit_circuit.h(0) + gate = GlobalPhaseGate(1.23) + qiskit_circuit.append(gate, []) braket_circuit = to_braket(qiskit_circuit) - - expected_braket_circuit = Circuit().h(0).gphase(np.pi / 2) - - self.assertEqual(braket_circuit.global_phase, qiskit_circuit.global_phase) + expected_braket_circuit = Circuit().h(0).gphase(1.23).gphase(np.pi / 2) + self.assertEqual( + braket_circuit.global_phase, qiskit_circuit.global_phase + gate.params[0] + ) self.assertEqual(braket_circuit, expected_braket_circuit) + braket_circuit_no_gphase = to_braket(qiskit_circuit, basis_gates={"h"}) + self.assertEqual(braket_circuit_no_gphase.global_phase, 0) + self.assertEqual(braket_circuit_no_gphase, Circuit().h(0)) + def test_exponential_gate_decomp(self): """Tests adapter translation of exponential gates""" aer_backend = BasicAer.get_backend("statevector_simulator") @@ -303,6 +309,7 @@ def test_mappers(self): "rxx": "xx", "ryy": "yy", "zz": "zz", + "global_phase": "gphase", } qiskit_to_braket_gate_names |= {