-
Notifications
You must be signed in to change notification settings - Fork 22
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
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3295f21
another small example of variation
chensgit169 6a1a249
add QuantumRegister
chensgit169 9646ce4
Merge remote-tracking branch 'origin/master'
chensgit169 042b4cc
Merge branch 'ScQ-Cloud:master' into master
chensgit169 464046b
avoid plotting of unused-qubits
chensgit169 dd73687
Merge remote-tracking branch 'origin/master'
chensgit169 b6d7045
avoid plotting of unused-qubits
chensgit169 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,5 +27,3 @@ def plot(): | |
show=True, | ||
save=False, | ||
) | ||
|
||
plot() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggest not to put test/example code here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this part has been removed. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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__
There was a problem hiding this comment.
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.