Skip to content

Commit acbe710

Browse files
[CQT-437] Separate arguments into parameters (or arguments) and operands (#653)
1 parent da8ea4d commit acbe710

29 files changed

+117
-119
lines changed

opensquirrel/circuit_builder.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from opensquirrel.circuit import Circuit
1010
from opensquirrel.default_instructions import default_instruction_set
11-
from opensquirrel.ir import IR, AsmDeclaration, Bit, BitLike, Instruction, Measure, Qubit, QubitLike
11+
from opensquirrel.ir import IR, AsmDeclaration, Bit, BitLike, Instruction, Qubit, QubitLike
1212
from opensquirrel.register_manager import BitRegister, QubitRegister, RegisterManager
1313

1414
_builder_dynamic_attributes = (*default_instruction_set, "asm")
@@ -75,12 +75,11 @@ def _check_bit_out_of_bounds_access(self, bit: BitLike) -> None:
7575
raise IndexError(msg)
7676

7777
def _check_out_of_bounds_access(self, instruction: Instruction) -> None:
78-
for qubit in instruction.get_qubit_operands():
78+
for qubit in instruction.qubit_operands:
7979
self._check_qubit_out_of_bounds_access(qubit)
8080

81-
if isinstance(instruction, Measure):
82-
for bit in instruction.get_bit_operands():
83-
self._check_bit_out_of_bounds_access(bit)
81+
for bit in instruction.bit_operands:
82+
self._check_bit_out_of_bounds_access(bit)
8483

8584
def _add_statement(self, attr: str, *args: Any) -> Self:
8685
if attr == "asm":

opensquirrel/ir/control_instruction.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from abc import ABC, abstractmethod
22
from typing import Any, SupportsInt
33

4-
from opensquirrel.ir.expression import Expression, Int, Qubit, QubitLike
4+
from opensquirrel.ir.expression import Bit, Expression, Int, Qubit, QubitLike
55
from opensquirrel.ir.ir import IRVisitor
66
from opensquirrel.ir.statement import Instruction
77

@@ -16,8 +16,13 @@ def __init__(self, qubit: QubitLike, name: str) -> None:
1616
def arguments(self) -> tuple[Expression, ...]:
1717
pass
1818

19-
def get_qubit_operands(self) -> list[Qubit]:
20-
return [self.qubit]
19+
@property
20+
def qubit_operands(self) -> tuple[Qubit, ...]:
21+
return (self.qubit,)
22+
23+
@property
24+
def bit_operands(self) -> tuple[Bit, ...]:
25+
return ()
2126

2227

2328
class Barrier(ControlInstruction):

opensquirrel/ir/default_gates/two_qubit_gates.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,6 @@ def __init__(self, qubit_0: QubitLike, qubit_1: QubitLike) -> None:
4141
self.qubit_0 = Qubit(qubit_0)
4242
self.qubit_1 = Qubit(qubit_1)
4343

44-
@property
45-
def arguments(self) -> tuple[Expression, ...]:
46-
return self.qubit_0, self.qubit_1
47-
48-
def get_qubit_operands(self) -> list[Qubit]:
49-
return [self.qubit_0, self.qubit_1]
50-
5144

5245
class CNOT(TwoQubitGate):
5346
def __init__(self, control_qubit: QubitLike, target_qubit: QubitLike) -> None:

opensquirrel/ir/non_unitary.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,13 @@ def __init__(self, qubit: QubitLike, name: str) -> None:
1919
def arguments(self) -> tuple[Expression, ...]:
2020
pass
2121

22-
def get_qubit_operands(self) -> list[Qubit]:
23-
return [self.qubit]
22+
@property
23+
def qubit_operands(self) -> tuple[Qubit, ...]:
24+
return (self.qubit,)
25+
26+
@property
27+
def bit_operands(self) -> tuple[Bit, ...]:
28+
return ()
2429

2530
def accept(self, visitor: IRVisitor) -> Any:
2631
return visitor.visit_non_unitary(self)
@@ -49,8 +54,9 @@ def accept(self, visitor: IRVisitor) -> Any:
4954
non_unitary_visit = super().accept(visitor)
5055
return non_unitary_visit if non_unitary_visit is not None else visitor.visit_measure(self)
5156

52-
def get_bit_operands(self) -> list[Bit]:
53-
return [self.bit]
57+
@property
58+
def bit_operands(self) -> tuple[Bit, ...]:
59+
return (self.bit,)
5460

5561

5662
class Init(NonUnitary):

opensquirrel/ir/single_qubit_gate.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,9 @@ def __mul__(self, other: SingleQubitGate) -> SingleQubitGate:
8484
return SingleQubitGate(self.qubit, self.bsr * other.bsr)
8585

8686
@property
87-
def arguments(self) -> tuple[Qubit, ...]:
87+
def qubit_operands(self) -> tuple[Qubit, ...]:
8888
return (self.qubit,)
8989

90-
def get_qubit_operands(self) -> list[Qubit]:
91-
return [self.qubit]
92-
9390
def is_identity(self) -> bool:
9491
if self.bsr is not None:
9592
return self.bsr.is_identity()

opensquirrel/ir/statement.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from abc import ABC, abstractmethod
22
from typing import Any
33

4-
from opensquirrel.ir.expression import Expression, Qubit, String, SupportsStr
4+
from opensquirrel.ir.expression import Bit, Expression, Qubit, String, SupportsStr
55
from opensquirrel.ir.ir import IRNode, IRVisitor
66

77

@@ -40,8 +40,18 @@ def __init__(self, name: str) -> None:
4040
def arguments(self) -> tuple[Expression, ...]:
4141
pass
4242

43+
@property
44+
@abstractmethod
45+
def qubit_operands(self) -> tuple[Qubit, ...]:
46+
pass
47+
48+
@property
49+
def qubit_indices(self) -> list[int]:
50+
return [qubit.index for qubit in self.qubit_operands]
51+
52+
@property
4353
@abstractmethod
44-
def get_qubit_operands(self) -> list[Qubit]:
54+
def bit_operands(self) -> tuple[Bit, ...]:
4555
pass
4656

4757
def accept(self, visitor: IRVisitor) -> Any:

opensquirrel/ir/two_qubit_gate.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import numpy as np
55

66
from opensquirrel.ir import Gate, IRVisitor, Qubit, QubitLike
7-
from opensquirrel.ir.expression import Expression
87
from opensquirrel.ir.semantics import CanonicalGateSemantic, ControlledGateSemantic, MatrixGateSemantic
98
from opensquirrel.ir.semantics.gate_semantic import GateSemantic
109
from opensquirrel.utils import get_matrix
@@ -27,7 +26,7 @@ def __init__(
2726
msg = "the qubit from the target gate does not match with 'qubit1'."
2827
raise ValueError(msg)
2928

30-
if self._check_repeated_qubit_operands([self.qubit0, self.qubit1]):
29+
if self._check_repeated_qubit_operands(self.qubit_operands):
3130
msg = "qubit0 and qubit1 cannot be the same"
3231
raise ValueError(msg)
3332

@@ -62,12 +61,9 @@ def accept(self, visitor: IRVisitor) -> Any:
6261
return visit_parent if visit_parent is not None else visitor.visit_two_qubit_gate(self)
6362

6463
@property
65-
def arguments(self) -> tuple[Expression, ...]:
64+
def qubit_operands(self) -> tuple[Qubit, ...]:
6665
return (self.qubit0, self.qubit1)
6766

68-
def get_qubit_operands(self) -> list[Qubit]:
69-
return [self.qubit0, self.qubit1]
70-
7167
def is_identity(self) -> bool:
7268
if self.controlled:
7369
return self.controlled.is_identity()

opensquirrel/ir/unitary.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
from opensquirrel.ir.statement import Instruction
99

1010
if TYPE_CHECKING:
11-
from opensquirrel.ir import IRVisitor
12-
from opensquirrel.ir.expression import Qubit
11+
from opensquirrel.ir import Bit, IRVisitor, Qubit
12+
from opensquirrel.ir.expression import Expression
1313

1414

1515
class Unitary(Instruction, ABC):
@@ -25,14 +25,18 @@ def __init__(self, name: str) -> None:
2525
def _check_repeated_qubit_operands(qubits: Sequence[Qubit]) -> bool:
2626
return len(qubits) != len(set(qubits))
2727

28-
@abstractmethod
29-
def get_qubit_operands(self) -> list[Qubit]:
30-
pass
31-
3228
@abstractmethod
3329
def is_identity(self) -> bool:
3430
pass
3531

32+
@property
33+
def arguments(self) -> tuple[Expression, ...]:
34+
return ()
35+
36+
@property
37+
def bit_operands(self) -> tuple[Bit, ...]:
38+
return ()
39+
3640
def accept(self, visitor: IRVisitor) -> Any:
3741
return visitor.visit_gate(self)
3842

@@ -43,7 +47,7 @@ def __eq__(self, other: object) -> bool:
4347

4448

4549
def compare_gates(g1: Gate, g2: Gate) -> bool:
46-
union_mapping = [q.index for q in list(set(g1.get_qubit_operands()) | set(g2.get_qubit_operands()))]
50+
union_mapping = list(set(g1.qubit_indices) | set(g2.qubit_indices))
4751

4852
from opensquirrel.circuit_matrix_calculator import get_circuit_matrix
4953
from opensquirrel.reindexer import get_reindexed_circuit

opensquirrel/passes/decomposer/cnot2cz_decomposer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def decompose(self, gate: Gate) -> list[Gate]:
2525
if gate.name != "CNOT":
2626
return [gate]
2727

28-
control_qubit, target_qubit = gate.get_qubit_operands()
28+
control_qubit, target_qubit = gate.qubit_operands
2929
return [
3030
Ry(target_qubit, -pi / 2),
3131
CZ(control_qubit, target_qubit),

opensquirrel/passes/decomposer/cz_decomposer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def decompose(self, g: Gate) -> list[Gate]:
3232
# - decomposing MatrixGate is currently not supported.
3333
return [g]
3434

35-
control_qubit, target_qubit = g.get_qubit_operands()
35+
control_qubit, target_qubit = g.qubit_operands
3636
target_gate = g.controlled.target_gate
3737

3838
# Perform XYX decomposition on the target gate.

0 commit comments

Comments
 (0)