Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cls Qubit and QuantumRegister #46

Merged
merged 7 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
34 changes: 34 additions & 0 deletions src/quafu/benchmark/variational/brickwall_circuit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from quafu.circuits.quantum_circuit import QuantumCircuit
from numpy import random

qubit_num = 6
n_layers = 2


def brickwall_layout_circuit(params, pbc=False):
"""
`params` is for circuit trainable parameters
"""
c = QuantumCircuit(qubit_num)
offset = 0 if pbc else 1
for j in range(n_layers):
for i in range(0, qubit_num - offset, 2):
c.cnot(i, (i + 1) % qubit_num)
for i in range(qubit_num):
c.rx(i, params[j, i, 0])
for i in range(1, qubit_num - offset, 2):
c.cnot(i, (i + 1) % qubit_num)
for i in range(qubit_num):
c.rx(i, params[j, i, 1])
return c


def plot():
para = random.random((n_layers, qubit_num, 2))
qc = brickwall_layout_circuit(para)
qc.plot_circuit(title='Brickwall Layout for \nVariational Circuit',
show=True,
save=False,
)

plot()
2 changes: 0 additions & 2 deletions src/quafu/benchmark/variational/ladder_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,3 @@ def plot():
show=True,
save=False,
)

plot()
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)
Copy link
Collaborator

@Zhaoyilunnn Zhaoyilunnn Aug 2, 2023

Choose a reason for hiding this comment

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

Can we have an inplace method, i.e., not to create a new cls
sth like this: self.qubits.update({i + len(self): qubit for i, qubit in other.qubits.items()}) , define an __iadd__

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Of course, yet this function was merely defined here as a reminder for future development. Let's see whether it will be actually needed.


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])
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggest not to put test/example code here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, this part has been removed.