Skip to content

Commit

Permalink
sum of two qcircuit
Browse files Browse the repository at this point in the history
  • Loading branch information
dakk committed Oct 26, 2023
1 parent 16ef190 commit a8921d8
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 45 deletions.
10 changes: 5 additions & 5 deletions qlasskit/qcircuit/gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ def __init__(self, gate, n_controls):
def apply(gate: QGate, qubits: List[int], param=None):
if len(qubits) != gate.n_qubits:
raise Exception(f"expected {gate.n_qubits} qubits ({len(qubits)} given)")
return [gate, qubits, param]
return (
gate,
qubits,
param,
)


AppliedGate = Tuple[QGate, List[int], Any]

# qc.append(CX(), [0, 1])
# qc += apply(CX(), [0, 1])
# qc += another_circ
27 changes: 25 additions & 2 deletions qlasskit/qcircuit/qcircuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@

# http://www.apache.org/licenses/LICENSE-2.0

import copy

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Any, List, Literal, Union
from typing import Any, List, Literal, Tuple, Union

from sympy import Symbol

Expand Down Expand Up @@ -78,7 +80,28 @@ def __getitem__(self, key: Union[str, Symbol, int]):

def __add__(self, qc: "QCircuit") -> "QCircuit":
"""Create a new QCircuit that merges two"""
raise Exception("not implemented")
nqc = copy.deepcopy(self)
nqc += qc
return nqc

def __iadd__(self, other: Union[gates.AppliedGate, "QCircuit"]): # type: ignore
"""AugAssign between a qcircuit and a AppliedGate|QCircuit"""
if isinstance(other, Tuple): # type: ignore
self.append(other[0], other[1], other[2])
elif isinstance(other, QCircuit): # type: ignore
if other.num_qubits > self.num_qubits:
raise Exception(
f"Other circuit has too many qubits {other.num_qubits} > {self.num_qubits}"
)

self.gates.extend(other.gates)
self.gates_computed.extend(other.gates_computed)

for s, q in other.qubit_map.items():
if s not in self.qubit_map:
self.qubit_map[s] = q

return self

def add_qubit(self, name=None):
"""Add a qubit to the circuit.
Expand Down
37 changes: 0 additions & 37 deletions qlasskit/utils.py

This file was deleted.

13 changes: 12 additions & 1 deletion test/test_qcircuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def test_base_mapping(self):
qc.ccx("a", Symbol("b"), c)
self.assertEqual(qc.num_qubits, 3)
self.assertTrue(isinstance(qc.gates[0][0], gates.CCX))
self.assertEqual(qc.gates[0][1:], [[0, 1, 2], None])
self.assertEqual(qc.gates[0][1:], ([0, 1, 2], None))

def test_duplicate_qubit(self):
qc = QCircuit()
Expand All @@ -47,6 +47,17 @@ def test_mapping(self):
qc = QCircuit(4)
qc.ccx("q0", "q1", "q2")

def test_augassign(self):
qc = QCircuit(1)
qc += gates.apply(gates.X(), [0])

def test_augassign_othercirc(self):
qc = QCircuit(1)
qc += gates.apply(gates.X(), [0])

qc2 = QCircuit(1)
qc2 += qc

def test_get_key_by_index(self):
qc = QCircuit()
a, b = qc.add_qubit("a"), qc.add_qubit("b")
Expand Down

0 comments on commit a8921d8

Please sign in to comment.