Skip to content

Commit

Permalink
Merge pull request #106 from beizhansl/patch-1
Browse files Browse the repository at this point in the history
Add support for classical operation
  • Loading branch information
chensgit169 authored Nov 3, 2023
2 parents e507697 + dedd046 commit a0063c1
Show file tree
Hide file tree
Showing 21 changed files with 1,698 additions and 412 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/wheels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,12 @@ jobs:

# Used to host cibuildwheel
- uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}

- name: Install dependence
run: python -m pip install pybind11 cibuildwheel scikit-build twine pytest

- name: Build wheels
run: python -m cibuildwheel --output-dir dist

Expand Down
2 changes: 1 addition & 1 deletion quafu/algorithms/ansatz.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def _build(self):
for pauli_str in self._pauli_list:
gate_list = self._evol.evol(pauli_str, self._gamma[layer])
for g in gate_list:
self.add_gate(g)
self.add_ins(g)

# Add H_B layer
for i in range(self.num):
Expand Down
1 change: 1 addition & 0 deletions quafu/circuits/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

from .quantum_circuit import QuantumCircuit
from .quantum_register import QuantumRegister
from .classical_register import ClassicalRegister
50 changes: 50 additions & 0 deletions quafu/circuits/classical_register.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# (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 ClassicalRegister:
"""
Collection of cbit(s)
"""

def __init__(self, num: int = 0, name: str = None):
self.name = name
self.num = num
self.cbits = {i: 0 for i in range(num)}
self.pos_start = 0

def __getitem__(self, item):
"""Get mapped global pos"""
return self.pos_start + item

def __len__(self):
return self.num

@property
def value(self, item:int=None):
"""Get value stored in register"""
if item is None:
return self.cbits
if item >= self.num or item < 0:
raise Exception(f"index {item} out of range.")
return self.cbits[item]

def __add__(self, other: "ClassicalRegister"):
creg = ClassicalRegister(name=self.name)
creg.cbits = {
**{self.cbits},
**{i + len(self): cbit for i, cbit in other.cbits.item()},
}
creg.num = self.num + other.num
creg.pos_start = self.pos_start
return creg
Loading

0 comments on commit a0063c1

Please sign in to comment.