Skip to content

Commit

Permalink
Merge pull request #244 from QunaSys/bug_fix_add_gate
Browse files Browse the repository at this point in the history
bug fix: correct adding gate in front of the circuit
  • Loading branch information
toru4838 committed Oct 20, 2023
2 parents d342729 + cbcb80f commit 757b7c9
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/circuit/quri_parts/circuit/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ def add_gate(self, gate: QuantumGate, gate_index: Optional[int] = None) -> None:
"than cbit_count",
)

if gate_index:
if gate_index is not None:
self._gates.insert(gate_index, gate)
else:
self._gates.append(gate)
Expand Down
2 changes: 1 addition & 1 deletion packages/circuit/quri_parts/circuit/circuit_parametric.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ def add_gate(self, gate: QuantumGate, gate_index: Optional[int] = None) -> None:
raise ValueError(
"The indices of the gate applied must be smaller than qubit_count"
)
if gate_index:
if gate_index is not None:
self._gates.insert(gate_index, (gate, None))
else:
self._gates.append((gate, None))
Expand Down
4 changes: 4 additions & 0 deletions packages/circuit/tests/circuit/test_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ def test_add_gate(self) -> None:
with pytest.raises(ValueError):
circuit.add_gate(X(3))

circuit.add_gate(X(0), 0)
assert circuit.gates == (X(0),) + tuple(_GATES)
assert len(circuit.gates) == len(_GATES) + 1

def test_get_mutable_copy(self) -> None:
circuit = mutable_circuit()
circuit_copied = circuit.get_mutable_copy()
Expand Down
14 changes: 14 additions & 0 deletions packages/circuit/tests/circuit/test_circuit_parametric.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
RX,
ImmutableBoundParametricQuantumCircuit,
ImmutableUnboundParametricQuantumCircuit,
ParametricPauliRotation,
ParametricRX,
QuantumCircuit,
QuantumGate,
UnboundParametricQuantumCircuit,
Expand Down Expand Up @@ -117,6 +119,18 @@ def test_radd(self) -> None:
exp_circuit.add_ParametricPauliRotation_gate([1], [1])
assert got_circuit.gates == exp_circuit.gates

def test_add_gate(self) -> None:
circuit = mutable_circuit()
assert circuit.gates == _GATES + [
ParametricRX(0),
ParametricPauliRotation([1], [1]),
]
circuit.add_gate(X(0), 0)
assert circuit.gates == [X(0)] + _GATES + [
ParametricRX(0),
ParametricPauliRotation([1], [1]),
]


class TestImmutableUnboundParametricQuantumCircuit:
def test_immutable_unbound_parametric_quantum_circuit(self) -> None:
Expand Down

1 comment on commit 757b7c9

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.