Skip to content

Commit

Permalink
add QuantumRegister
Browse files Browse the repository at this point in the history
  • Loading branch information
chensgit169 committed Aug 1, 2023
1 parent 3295f21 commit 6a1a249
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions src/quafu/circuits/qreg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# (C) Copyright 2023 Beijing Academy of Quantum Information Sciences
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# 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.

class Qubit:
"""
Representation of logical qubits.
"""
def __init__(self, logic_pos: int, reg_name: str = None, label: str = None, ):
self.pos = logic_pos
self.reg_name = 'q' if reg_name is None else reg_name
self.label = self.reg_name if label is None else label
self.physical_info = None
self._depth = 0 # present depth

def __repr__(self):
return self.reg_name + '_%s' % self.pos

def load_physical_info(self, *args, **kwargs):
raise NotImplementedError

@property
def used(self):
return self._depth > 0

def add_depth(self, num: int = 1):
self._depth += num

def move_pos(self, new_pos):
old_pos = 0 + self.pos
self.pos = new_pos
return old_pos


class QuantumRegister:
"""
Collection of Qubit(s)
"""
def __init__(self, num: int = 0, name: str = None):
self.name = name
self.qubits = {i: Qubit(logic_pos=i, reg_name=name) for i in range(num)}

def __getitem__(self, item):
return self.qubits[item]

def __len__(self):
return len(self.qubits)

def __add__(self, other: 'QuantumRegister'):
qreg = QuantumRegister(name=self.name)
qreg.qubits = {**{self.qubits}, **{i + len(self): qubit for i, qubit in other.qubits.item()}}
return QuantumRegister(len(self) + len(other), name=self.name)

def exchange(self, p1, p2):
pass


if __name__ == '__main__':
# q0 = Qubit(0)
# q1 = Qubit(1)
# reg = {q.pos: q for q in [q0, q1]}
# print(reg)
#
# reg[0], reg[1] = reg[1], reg[0]
# reg[0].pos, reg[1].pos = reg[1].pos, reg[0].pos
#
# print(reg)
# print([q0.pos, q1.pos])
reg = QuantumRegister(4, name='reg')
print(reg[3])

0 comments on commit 6a1a249

Please sign in to comment.