Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/braket/circuits/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from __future__ import annotations

import warnings
from collections import Counter
from collections.abc import Callable, Iterable, Sequence
from numbers import Number
Expand Down Expand Up @@ -1311,13 +1312,24 @@ def diagram(self, circuit_diagram_class: type = UnicodeCircuitDiagram) -> str:

def to_ir(
self,
ir_type: IRType = IRType.JAQCD,
ir_type: IRType = IRType.OPENQASM,
serialization_properties: SerializationProperties | None = None,
gate_definitions: dict[tuple[Gate, QubitSet], PulseSequence] | None = None,
) -> OpenQasmProgram | JaqcdProgram:
"""Converts the circuit into the canonical intermediate representation.
If the circuit is sent over the wire, this method is called before it is sent.

.. versionchanged:: 1.117.4
The default ``ir_type`` is now :attr:`IRType.OPENQASM` (previously
:attr:`IRType.JAQCD`). Amazon Braket service devices no longer
accept JAQCD program submissions; see MCM-150287739.

.. deprecated:: 1.117.4
Passing ``ir_type=IRType.JAQCD`` is deprecated and emits a
``UserWarning``. Use :attr:`IRType.OPENQASM` instead. The
``IRType.JAQCD`` enum member and the JAQCD conversion path are
retained so historical task results continue to deserialize.

Args:
ir_type (IRType): The IRType to use for converting the circuit object to its
IR representation.
Expand All @@ -1337,6 +1349,12 @@ def to_ir(
"""
gate_definitions = gate_definitions or {}
if ir_type == IRType.JAQCD:
warnings.warn(
"JAQCD IR is deprecated and will be removed in a future release. "
"Use IRType.OPENQASM instead. Amazon Braket service devices no "
"longer accept JAQCD program submissions. See MCM-150287739.",
stacklevel=2,
)
return self._to_jaqcd()
if ir_type == IRType.OPENQASM:
if serialization_properties and not isinstance(
Expand Down
10 changes: 9 additions & 1 deletion src/braket/circuits/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@


class IRType(StrEnum):
"""Defines the available IRTypes for circuit serialization."""
"""Defines the available IRTypes for circuit serialization.

.. deprecated:: 1.117.4
``JAQCD`` is deprecated. Amazon Braket service devices no longer accept
JAQCD program submissions; use ``OPENQASM`` instead. The ``JAQCD``
enum member is retained so circuits can still be converted to JAQCD
for legacy code paths and historical task-result parsing. See
MCM-150287739.
"""

OPENQASM = "OPENQASM"
JAQCD = "JAQCD"
Expand Down
52 changes: 46 additions & 6 deletions test/unit_tests/braket/circuits/test_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.

import warnings
from unittest.mock import Mock

import numpy as np
Expand Down Expand Up @@ -1184,9 +1185,9 @@ def h_nested(target):

def test_ir_empty_instructions_result_types():
circ = Circuit()
assert circ.to_ir() == jaqcd.Program(
instructions=[], results=[], basis_rotation_instructions=[]
)
with pytest.warns(UserWarning, match="JAQCD"):
ir = circ.to_ir(IRType.JAQCD)
assert ir == jaqcd.Program(instructions=[], results=[], basis_rotation_instructions=[])


def test_ir_non_empty_instructions_result_types():
Expand All @@ -1196,7 +1197,9 @@ def test_ir_non_empty_instructions_result_types():
results=[jaqcd.Probability(targets=[0, 1])],
basis_rotation_instructions=[],
)
assert circ.to_ir() == expected
with pytest.warns(UserWarning, match="JAQCD"):
ir = circ.to_ir(IRType.JAQCD)
assert ir == expected


def test_ir_non_empty_instructions_result_types_basis_rotation_instructions():
Expand All @@ -1206,7 +1209,41 @@ def test_ir_non_empty_instructions_result_types_basis_rotation_instructions():
results=[jaqcd.Sample(observable=["x"], targets=[0])],
basis_rotation_instructions=[jaqcd.H(target=0)],
)
assert circ.to_ir() == expected
with pytest.warns(UserWarning, match="JAQCD"):
ir = circ.to_ir(IRType.JAQCD)
assert ir == expected


def test_to_ir_default_is_openqasm():
"""Calling Circuit.to_ir() with no ir_type argument should return an
OpenQASM program (after the JAQCD-deprecation default flip)."""
circ = Circuit().h(0).cnot(0, 1)
assert isinstance(circ.to_ir(), OpenQasmProgram)


def test_to_ir_jaqcd_emits_warning():
"""Passing IRType.JAQCD explicitly should emit a UserWarning that
references JAQCD and points the customer at the migration path."""
circ = Circuit().h(0)
with pytest.warns(UserWarning, match="JAQCD"):
out = circ.to_ir(IRType.JAQCD)
# The actual JAQCD program is still produced — only the warning is new.
assert out == jaqcd.Program(
instructions=[jaqcd.H(target=0)], results=[], basis_rotation_instructions=[]
)


def test_to_ir_default_emits_no_jaqcd_warning():
"""The new default (OpenQASM) must not emit the JAQCD deprecation
warning. We assert specifically on JAQCD-message warnings rather than
erroring on all UserWarnings, since other unrelated warnings may fire
legitimately."""
circ = Circuit().h(0)
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
circ.to_ir()
jaqcd_warnings = [x for x in w if "JAQCD" in str(x.message)]
assert jaqcd_warnings == []


@pytest.mark.parametrize(
Expand Down Expand Up @@ -3815,5 +3852,8 @@ def test_barrier_openqasm_export_all_qubits():

def test_barrier_jaqcd_export_fails():
circ = Circuit().h(0).barrier([0, 1])
with pytest.raises(NotImplementedError, match="Barrier is not supported in JAQCD"):
with (
pytest.warns(UserWarning, match="JAQCD"),
pytest.raises(NotImplementedError, match="Barrier is not supported in JAQCD"),
):
circ.to_ir(IRType.JAQCD)
6 changes: 5 additions & 1 deletion test/unit_tests/braket/devices/test_local_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,11 @@ def test_run_program_model_inputs():
def test_run_jaqcd_only():
dummy = DummyJaqcdSimulator()
sim = LocalSimulator(dummy)
task = sim.run(Circuit().h(0).cnot(0, 1), 10)
# When the local simulator advertises only JAQCD (no OpenQASM), the
# LocalSimulator payload constructor falls back to converting the
# circuit to JAQCD, which (per the JAQCD deprecation) emits a UserWarning.
with pytest.warns(UserWarning, match="JAQCD"):
task = sim.run(Circuit().h(0).cnot(0, 1), 10)
dummy.assert_shots(10)
dummy.assert_qubits(None)
assert task.result() == GateModelQuantumTaskResult.from_object(GATE_MODEL_RESULT)
Expand Down
Loading