Skip to content

Commit

Permalink
Merge pull request #86 from beizhansl/master
Browse files Browse the repository at this point in the history
Implement PLY Convertor
  • Loading branch information
Zhaoyilunnn authored Sep 16, 2023
2 parents 5ca8027 + 494c7e3 commit 229ebb5
Show file tree
Hide file tree
Showing 14 changed files with 3,195 additions and 425 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ MANIFEST.in
*.pyd
dev_*.py
*temp.*
.idea
parsetab.py
17 changes: 16 additions & 1 deletion quafu/circuits/quantum_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import quafu.elements.element_gates as qeg
from quafu.elements.quantum_element.pulses.quantum_pulse import QuantumPulse
from quafu.elements.quantum_element.quantum_element import Reset
from ..elements.quantum_element import (
Barrier,
Delay,
Expand All @@ -44,6 +45,7 @@ def __init__(self, num: int, *args, **kwargs):
self.openqasm = ""
self.circuit = []
self.measures = {}
self.executable_on_backend = True
self._used_qubits = []
self._parameterized_gates = []

Expand Down Expand Up @@ -639,6 +641,19 @@ def delay(self, pos, duration, unit="ns") -> "QuantumCircuit":
self.add_gate(Delay(pos, duration, unit=unit))
return self

def reset(self, qlist:List[int]= None) -> "QuantumCircuit":
"""
Add reset for qubits in qlist.
Args:
qlist (list[int]): A list contain the qubit need add reset. When qlist contain at least two qubit, the barrier will be added from minimum qubit to maximum qubit. For example: barrier([0, 2]) create barrier for qubits 0, 1, 2. To create discrete barrier, using barrier([0]), barrier([2]).
"""
if qlist is None:
qlist = list(range(self.num))
self.add_gate(Reset(qlist))
self.executable_on_backend = False
return self

def xy(self, qs: int, qe: int, duration: int, unit: str = "ns") -> "QuantumCircuit":
"""
XY resonance time evolution for quantum simulator
Expand Down Expand Up @@ -722,7 +737,7 @@ def unitary(self, matrix: np.ndarray, pos: List[int]):
"""
compiler = qeg.UnitaryDecomposer(array=matrix, qubits=pos)
compiler.apply_to_qc(self)

def measure(self, pos: List[int] = None, cbits: List[int] = None) -> None:
"""
Measurement setting for experiment device.
Expand Down
22 changes: 22 additions & 0 deletions quafu/elements/quantum_element/classical_element.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# (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 Cif:
name = 'if'
pos = 0
def __init__(self, cbit, condition, instructions):
# cbit can be a list of cbit or just a cbit
self.cbit = cbit
self.cond = condition
self.instructions = instructions
24 changes: 24 additions & 0 deletions quafu/elements/quantum_element/quantum_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,29 @@ def to_qasm(self):
)


class Reset(Instruction):
name = 'reset'

def __init__(self, pos):
super().__init__(pos)

@property
def pos(self):
return self.__pos

@pos.setter
def pos(self, pos):
self.__pos = pos

def __repr__(self):
return f"{self.__class__.__name__}"

def to_qasm(self):
return "reset " + ",".join(
["q[%d]" % p for p in range(min(self.pos), max(self.pos) + 1)]
)


class Delay(Instruction):
name = "delay"

Expand Down Expand Up @@ -94,3 +117,4 @@ def __init__(self, bitmap: dict):
Instruction.register_ins(Delay)
Instruction.register_ins(XYResonance)
Instruction.register_ins(Measure)
Instruction.register_ins(Reset)
23 changes: 21 additions & 2 deletions quafu/qfasm/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,35 @@
# (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.

from quafu.exceptions import QuafuError


class QfasmError(QuafuError):
"""
Base class for errors raised by Qfasm.
"""

pass


class QfasmSyntaxError(QfasmError):
class LexerError(Exception):
"""Errors raised while lexer get tokens"""

pass


class QfasmSemanticError(QfasmError):
class ParserError(Exception):
"""Errors raised while parser OPENQASM"""

pass
Loading

0 comments on commit 229ebb5

Please sign in to comment.