Skip to content
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ arbitrarily applied mapper pass.
- `Can2CZDecomposer` to decompose arbitrary two-qubit gates.
- The following 2-qubit gates: `CV`, `CY`, `DCNOT`, `ECR`, `ISWAP`, `InvSqrtSWAP`, `M`, `MS`, `SqrtISWAP`, and `SqrtSWAP`
- Add `add_instruction` method to the `CircuitBuilder`
- libQASM parser accepts measure instruction aliases: `measureX`, `measureY`, and `measureZ`

## [ 0.9.0 ] - [ 2025-12-19 ]

Expand Down
17 changes: 14 additions & 3 deletions opensquirrel/reader/libqasm_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from collections import OrderedDict
from collections.abc import Callable
from typing import TYPE_CHECKING, Any, cast, overload
from typing import TYPE_CHECKING, Any, ClassVar, cast, overload

import cqasm.v3x as cqasm
import cqasm.v3x.types as cqasm_types
Expand Down Expand Up @@ -37,6 +37,12 @@


class LibQasmParser:
_MEASURE_ALIASES: ClassVar[dict[str, list[Float]]] = {
"measureX": [Float(1), Float(0), Float(0)],
"measureY": [Float(0), Float(1), Float(0)],
"measureZ": [Float(0), Float(0), Float(1)],
}

def __init__(self) -> None:
self.ir = IR()

Expand Down Expand Up @@ -181,8 +187,11 @@ def _get_expanded_measure_args(self, instruction: cqasm.semantic.NonGateInstruct
else:
msg = f"argument {ast_arg!r} is neither of qubit nor bit type"
raise TypeError(msg)
if instruction.parameters:

parameters = self._MEASURE_ALIASES.get(instruction.name)
if not parameters and instruction.parameters:
parameters = [self._ast_literal_to_ir_literal(parameter) for parameter in instruction.parameters]
if parameters:
number_of_operands = len(expanded_args[0])
expanded_args.append([parameters] * number_of_operands)
return list(zip(*expanded_args, strict=False))
Expand Down Expand Up @@ -217,6 +226,8 @@ def _get_gate_generator(self, instruction: cqasm.semantic.GateInstruction) -> Ca
def _get_non_gate_instruction_generator(
self, instruction: cqasm.semantic.NonGateInstruction
) -> Callable[..., NonUnitary | ControlInstruction]:
if instruction.name in self._MEASURE_ALIASES:
return lambda *args: default_non_unitary_set["measure"](*args)
if instruction.name in default_control_instruction_set:
return lambda *args: default_control_instruction_set[instruction.name](*args)
return lambda *args: default_non_unitary_set[instruction.name](*args)
Expand Down Expand Up @@ -302,7 +313,7 @@ def circuit_from_string(self, s: str) -> Circuit:
instruction_generator = self._get_non_gate_instruction_generator(statement)
expanded_args = (
self._get_expanded_measure_args(statement)
if statement.name == "measure"
if statement.name == "measure" or statement.name in self._MEASURE_ALIASES
else self._get_expanded_instruction_args(statement)
)
elif LibQasmParser._is_asm_declaration(statement):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ keywords = [
"compilation",
]
dependencies = [
"libqasm==1.4.0",
"libqasm==1.4.1",
"networkx>=3.4.2",
"numpy>=2.2.6",
"scipy>=1.15.3",
Expand Down
27 changes: 27 additions & 0 deletions tests/parser/libqasm/test_libqasm.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,33 @@ def test_parse_parameterized_measurement() -> None:
)


def test_parse_measurement_aliases() -> None:
circuit = LibQasmParser().circuit_from_string(
"""
version 3.0

qubit[3] q
bit[3] b

b[0] = measureX q[0]
b[1] = measureY q[1]
b[2] = measureZ q[2]
""",
)
assert (
str(circuit)
== """version 3.0

qubit[3] q
bit[3] b

b[0] = measure(1.0, 0.0, 0.0) q[0]
b[1] = measure(0.0, 1.0, 0.0) q[1]
b[2] = measure q[2]
"""
)


def test_sgmq() -> None:
circuit = LibQasmParser().circuit_from_string(
"""
Expand Down
44 changes: 22 additions & 22 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading