Skip to content

Commit 53aa5b8

Browse files
authored
deprecation: Circuit.to_ir default to OpenQASM, warn on JAQCD (#1266)
1 parent ec31c80 commit 53aa5b8

4 files changed

Lines changed: 29 additions & 15 deletions

File tree

src/braket/circuits/circuit.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
from __future__ import annotations
1515

16+
import warnings
1617
from collections import Counter
1718
from collections.abc import Callable, Iterable, Sequence
1819
from numbers import Number
@@ -1311,7 +1312,7 @@ def diagram(self, circuit_diagram_class: type = UnicodeCircuitDiagram) -> str:
13111312

13121313
def to_ir(
13131314
self,
1314-
ir_type: IRType = IRType.JAQCD,
1315+
ir_type: IRType = IRType.OPENQASM,
13151316
serialization_properties: SerializationProperties | None = None,
13161317
gate_definitions: dict[tuple[Gate, QubitSet], PulseSequence] | None = None,
13171318
) -> OpenQasmProgram | JaqcdProgram:
@@ -1337,6 +1338,10 @@ def to_ir(
13371338
"""
13381339
gate_definitions = gate_definitions or {}
13391340
if ir_type == IRType.JAQCD:
1341+
warnings.warn(
1342+
"The JAQCD action type is deprecated. Please use OpenQASM 3 programs instead.",
1343+
stacklevel=2,
1344+
)
13401345
return self._to_jaqcd()
13411346
if ir_type == IRType.OPENQASM:
13421347
if serialization_properties and not isinstance(

src/braket/devices/local_simulator.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,6 @@ def _(self, circuit: Circuit, inputs: dict[str, float] | None, shots: int):
262262
program = circuit.to_ir(ir_type=IRType.OPENQASM)
263263
program.inputs.update(inputs or {})
264264
return program
265-
if DeviceActionType.JAQCD in simulator.properties.action:
266-
validate_circuit_and_shots(circuit, shots)
267-
return circuit.to_ir(ir_type=IRType.JAQCD)
268265
raise NotImplementedError(f"{type(simulator)} does not support qubit gate-based programs")
269266

270267
@_construct_payload.register

test/unit_tests/braket/circuits/test_circuit.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,9 +1184,9 @@ def h_nested(target):
11841184

11851185
def test_ir_empty_instructions_result_types():
11861186
circ = Circuit()
1187-
assert circ.to_ir() == jaqcd.Program(
1188-
instructions=[], results=[], basis_rotation_instructions=[]
1189-
)
1187+
with pytest.warns(UserWarning, match="JAQCD"):
1188+
ir = circ.to_ir(IRType.JAQCD)
1189+
assert ir == jaqcd.Program(instructions=[], results=[], basis_rotation_instructions=[])
11901190

11911191

11921192
def test_ir_non_empty_instructions_result_types():
@@ -1196,7 +1196,9 @@ def test_ir_non_empty_instructions_result_types():
11961196
results=[jaqcd.Probability(targets=[0, 1])],
11971197
basis_rotation_instructions=[],
11981198
)
1199-
assert circ.to_ir() == expected
1199+
with pytest.warns(UserWarning, match="JAQCD"):
1200+
ir = circ.to_ir(IRType.JAQCD)
1201+
assert ir == expected
12001202

12011203

12021204
def test_ir_non_empty_instructions_result_types_basis_rotation_instructions():
@@ -1206,7 +1208,16 @@ def test_ir_non_empty_instructions_result_types_basis_rotation_instructions():
12061208
results=[jaqcd.Sample(observable=["x"], targets=[0])],
12071209
basis_rotation_instructions=[jaqcd.H(target=0)],
12081210
)
1209-
assert circ.to_ir() == expected
1211+
with pytest.warns(UserWarning, match="JAQCD"):
1212+
ir = circ.to_ir(IRType.JAQCD)
1213+
assert ir == expected
1214+
1215+
1216+
def test_to_ir_default_is_openqasm():
1217+
"""Calling Circuit.to_ir() with no ir_type argument should return an
1218+
OpenQASM program (after the JAQCD-deprecation default flip)."""
1219+
circ = Circuit().h(0).cnot(0, 1)
1220+
assert isinstance(circ.to_ir(), OpenQasmProgram)
12101221

12111222

12121223
@pytest.mark.parametrize(
@@ -3815,5 +3826,8 @@ def test_barrier_openqasm_export_all_qubits():
38153826

38163827
def test_barrier_jaqcd_export_fails():
38173828
circ = Circuit().h(0).barrier([0, 1])
3818-
with pytest.raises(NotImplementedError, match="Barrier is not supported in JAQCD"):
3829+
with (
3830+
pytest.warns(UserWarning, match="JAQCD"),
3831+
pytest.raises(NotImplementedError, match="Barrier is not supported in JAQCD"),
3832+
):
38193833
circ.to_ir(IRType.JAQCD)

test/unit_tests/braket/devices/test_local_simulator.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -689,13 +689,11 @@ def test_run_program_model_inputs():
689689
assert task.result() == GateModelQuantumTaskResult.from_object(GATE_MODEL_RESULT)
690690

691691

692-
def test_run_jaqcd_only():
692+
def test_run_jaqcd_only_raises():
693693
dummy = DummyJaqcdSimulator()
694694
sim = LocalSimulator(dummy)
695-
task = sim.run(Circuit().h(0).cnot(0, 1), 10)
696-
dummy.assert_shots(10)
697-
dummy.assert_qubits(None)
698-
assert task.result() == GateModelQuantumTaskResult.from_object(GATE_MODEL_RESULT)
695+
with pytest.raises(NotImplementedError, match="does not support qubit gate-based programs"):
696+
sim.run(Circuit().h(0).cnot(0, 1), 10)
699697

700698

701699
def test_run_program_model():

0 commit comments

Comments
 (0)