diff --git a/CHANGELOG.md b/CHANGELOG.md index b9c7fbd6..8e50acb8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ Types of changes: ### Removed ### Fixed +- `unroll()` accepts `inv @` on a custom gate whose body contains `nop`, and keeps each `nop` in the inverted body. Before, it raised `ValidationError: Unsupported / undeclared QASM operation: nop` ([#485](https://github.com/qBraid/pyqasm/pull/485), [#477](https://github.com/qBraid/pyqasm/issues/477)). - `publish-lab-env.yml` files a publish request before it approves the environment. Since qBraid/qbraid-api#1334 the server approves only an environment with an open request, so every run failed at approve with `Environment must have status REQUESTED or PENDING to publish`. The request counts toward the API key owner's publish quota and notifies the platform admins ([#491](https://github.com/qBraid/pyqasm/pull/491), [#490](https://github.com/qBraid/pyqasm/issues/490)). ### Dependencies diff --git a/src/pyqasm/maps/gates.py b/src/pyqasm/maps/gates.py index d6d5edbb..6e28f44b 100644 --- a/src/pyqasm/maps/gates.py +++ b/src/pyqasm/maps/gates.py @@ -1204,7 +1204,7 @@ def map_qasm_op_to_callable(op_node: QuantumGate) -> tuple[Callable, int]: # ty ) -SELF_INVERTING_ONE_QUBIT_OP_SET = {"id", "h", "x", "y", "z"} +SELF_INVERTING_ONE_QUBIT_OP_SET = {"id", "h", "x", "y", "z", "nop"} ST_GATE_INV_MAP = { "s": "sdg", "t": "tdg", diff --git a/src/pyqasm/visitor.py b/src/pyqasm/visitor.py index cd51f311..09f36c37 100644 --- a/src/pyqasm/visitor.py +++ b/src/pyqasm/visitor.py @@ -1400,11 +1400,10 @@ def _visit_custom_gate_operation( for formal_arg, actual_arg in zip(gate_definition.arguments, operation.arguments) } - gate_definition_ops = Qasm3Transformer.expand_nop_statements( - copy.deepcopy(gate_definition.body) - ) + gate_definition_ops = copy.deepcopy(gate_definition.body) if inverse: gate_definition_ops.reverse() + gate_definition_ops = Qasm3Transformer.expand_nop_statements(gate_definition_ops) self._scope_manager.push_scope({}) self._scope_manager.push_context(Context.GATE) diff --git a/tests/qasm3/test_gates.py b/tests/qasm3/test_gates.py index e2ee1576..894706aa 100644 --- a/tests/qasm3/test_gates.py +++ b/tests/qasm3/test_gates.py @@ -77,10 +77,19 @@ def test_single_qubit_qasm3_gates(circuit_name, request): ("nop q[0], q[0];", ["nop q[0];", "nop q[0];"]), ("gate g a { nop a; }\ng q[0];", ["nop q[0];"]), ("gate g a, b { nop a, b; x b; }\ng q[0], r[0];", ["nop q[0];", "nop r[0];", "x r[0];"]), + ("gate g a { nop a; }\ninv @ g q[0];", ["nop q[0];"]), + ( + "gate g a, b { nop a; x b; nop b; }\ninv @ g q[0], r[0];", + ["nop r[0];", "x r[0];", "nop q[0];"], + ), + ( + "gate g a, b { nop a, b; cx a, b; }\ninv @ g q[0], r[0];", + ["cx q[0], r[0];", "nop q[0];", "nop r[0];"], + ), ], ) def test_nop_unrolls_per_operand(statement, expected): - """A nop unrolls to one nop per operand qubit, also inside a gate definition. + """A nop unrolls to one nop per operand qubit, also inside a gate definition and its inverse. openqasm3 1.0.1 parses these as nop gate calls and 1.0.2 as nop statements, and both give the same unrolled program.