Skip to content

Commit

Permalink
Ignore global phase if unsupported (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
speller26 authored Feb 13, 2024
1 parent e29cbaf commit 157d7ab
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
28 changes: 21 additions & 7 deletions qiskit_braket_provider/providers/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

from qiskit_braket_provider.exception import QiskitBraketException

_GPHASE_GATE_NAME = "global_phase"

_BRAKET_TO_QISKIT_NAMES = {
"u": "u",
"phaseshift": "p",
Expand Down Expand Up @@ -63,6 +65,7 @@
"gpi": "gpi",
"gpi2": "gpi2",
"ms": "ms",
"gphase": _GPHASE_GATE_NAME,
}

_CONTROLLED_GATES_BY_QUBIT_COUNT = {
Expand Down Expand Up @@ -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] = {
Expand Down Expand Up @@ -174,6 +179,7 @@
Parameter("phi1") / (2 * pi),
Parameter("theta") / (2 * pi),
),
"gphase": qiskit_gates.GlobalPhaseGate(Parameter("theta")),
}


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion qiskit_braket_provider/version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""Qiskit-Braket provider version."""

__version__ = "0.1.0"
__version__ = "0.1.1"
17 changes: 12 additions & 5 deletions tests/providers/test_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -303,6 +309,7 @@ def test_mappers(self):
"rxx": "xx",
"ryy": "yy",
"zz": "zz",
"global_phase": "gphase",
}

qiskit_to_braket_gate_names |= {
Expand Down

0 comments on commit 157d7ab

Please sign in to comment.