Skip to content

Commit

Permalink
feat: merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
lucky9-cyou committed Dec 21, 2023
1 parent 9abb0b5 commit 3f93d55
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 65 deletions.
8 changes: 4 additions & 4 deletions quafu/dagcircuits/circuit_dag.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
from typing import Any, List

import networkx as nx
from quafu.dagcircuits.dag_circuit import (
from quafu.dagcircuits.dag_circuit import ( # dag_circuit.py in the same folder as circuit_dag.py now
DAGCircuit,
) # dag_circuit.py in the same folder as circuit_dag.py now
from quafu.dagcircuits.instruction_node import (
)
from quafu.dagcircuits.instruction_node import ( # instruction_node.py in the same folder as circuit_dag.py now
InstructionNode,
) # instruction_node.py in the same folder as circuit_dag.py now
)
from quafu.elements import Barrier, Delay, Measure, XYResonance
from quafu.elements.element_gates import *
from quafu.elements.pulses import FlattopPulse, GaussianPulse, RectPulse
Expand Down
15 changes: 7 additions & 8 deletions quafu/elements/element_gates/c11.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from ..quantum_gate import ControlledGate, FixedGate, QuantumGate
from abc import ABC
from typing import Dict

Expand All @@ -13,7 +12,7 @@ class _C11Gate(ControlledGate, ABC):
ct_dims = (1, 1, 2)


@QuantumGate.register('cx')
@QuantumGate.register("cx")
class CXGate(_C11Gate, FixedGate):
name = "CX"

Expand All @@ -22,23 +21,23 @@ def __init__(self, ctrl: int, targ: int):
self.symbol = "+"


@QuantumGate.register('cy')
@QuantumGate.register("cy")
class CYGate(_C11Gate, FixedGate):
name = "CY"

def __init__(self, ctrl: int, targ: int):
_C11Gate.__init__(self, "Y", [ctrl], [targ], None, tar_matrix=YMatrix)


@QuantumGate.register('cz')
@QuantumGate.register("cz")
class CZGate(_C11Gate, FixedGate):
name = "CZ"

def __init__(self, ctrl: int, targ: int):
_C11Gate.__init__(self, "Z", [ctrl], [targ], None, tar_matrix=ZMatrix)


@QuantumGate.register('cs')
@QuantumGate.register("cs")
class CSGate(_C11Gate, FixedGate):
name = "CS"

Expand All @@ -49,7 +48,7 @@ def to_qasm(self):
return "cp(pi/2) " + "q[%d],q[%d]" % (self.pos[0], self.pos[1])


@QuantumGate.register('ct')
@QuantumGate.register("ct")
class CTGate(_C11Gate, FixedGate):
name = "CT"

Expand All @@ -60,7 +59,7 @@ def to_qasm(self):
return "cp(pi/4) " + "q[%d],q[%d]" % (self.pos[0], self.pos[1])


@QuantumGate.register('cp')
@QuantumGate.register("cp")
class CPGate(_C11Gate):
name = "CP"

Expand All @@ -69,4 +68,4 @@ def __init__(self, ctrl: int, targ: int, paras):

@property
def named_paras(self) -> Dict:
return {'theta': self.paras}
return {"theta": self.paras}
8 changes: 5 additions & 3 deletions quafu/elements/element_gates/c12.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from ..quantum_gate import ControlledGate, FixedGate, QuantumGate
from quafu.elements.matrices import SwapMatrix

from ..quantum_gate import ControlledGate, FixedGate, QuantumGate

@QuantumGate.register('cswap')

@QuantumGate.register("cswap")
class FredkinGate(ControlledGate, FixedGate):
name = "CSWAP"

def __init__(self, ctrl: int, targ1: int, targ2: int):
ControlledGate.__init__(self, "SWAP", [ctrl], [targ1, targ2], None, tar_matrix=SwapMatrix)
ControlledGate.__init__(
self, "SWAP", [ctrl], [targ1, targ2], None, tar_matrix=SwapMatrix
)
11 changes: 5 additions & 6 deletions quafu/elements/element_gates/clifford.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import numpy as np
from quafu.elements.matrices import HMatrix, SMatrix
from ..quantum_gate import SingleQubitGate, FixedGate, QuantumGate

from ..quantum_gate import FixedGate, QuantumGate, SingleQubitGate

__all__ = ["HGate", "SGate", "SdgGate", "TGate", "TdgGate"]


@QuantumGate.register('h')
@QuantumGate.register("h")
class HGate(SingleQubitGate, FixedGate):
name = "H"
matrix = HMatrix
Expand All @@ -16,7 +15,7 @@ def __init__(self, pos: int):
FixedGate.__init__(self, pos)


@QuantumGate.register('s')
@QuantumGate.register("s")
class SGate(SingleQubitGate, FixedGate):
name = "S"
matrix = SMatrix
Expand All @@ -25,7 +24,7 @@ def __init__(self, pos: int):
FixedGate.__init__(self, pos)


@QuantumGate.register('sdg')
@QuantumGate.register("sdg")
class SdgGate(SingleQubitGate, FixedGate):
name = "Sdg"
matrix = SMatrix.conj().T
Expand All @@ -34,7 +33,7 @@ def __init__(self, pos: int):
FixedGate.__init__(self, pos)


@QuantumGate.register('t')
@QuantumGate.register("t")
class TGate(SingleQubitGate, FixedGate):
name = "T"
matrix = np.array([[1.0, 0.0], [0.0, np.exp(1.0j * np.pi / 4)]], dtype=complex)
Expand All @@ -43,7 +42,7 @@ def __init__(self, pos: int):
FixedGate.__init__(self, pos)


@QuantumGate.register('tdg')
@QuantumGate.register("tdg")
class TdgGate(SingleQubitGate, FixedGate):
name = "Tdg"
matrix = TGate.matrix.conj().T
Expand Down
13 changes: 7 additions & 6 deletions quafu/elements/element_gates/cm1.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,39 @@
from ..quantum_gate import ControlledGate, FixedGate, QuantumGate
from quafu.elements.matrices import XMatrix, YMatrix, ZMatrix

from ..quantum_gate import ControlledGate, FixedGate, QuantumGate

__all__ = ["MCXGate", "MCYGate", "MCZGate", "ToffoliGate"]


@QuantumGate.register('mcx')
@QuantumGate.register("mcx")
class MCXGate(ControlledGate, FixedGate):
name = "MCX"

def __init__(self, ctrls, targ: int):
ControlledGate.__init__(self, "X", ctrls, [targ], None, tar_matrix=XMatrix)


@QuantumGate.register('mcy')
@QuantumGate.register("mcy")
class MCYGate(ControlledGate, FixedGate):
name = "MCY"

def __init__(self, ctrls, targ: int):
ControlledGate.__init__(self, "Y", ctrls, [targ], None, tar_matrix=YMatrix)


@QuantumGate.register('mcz')
@QuantumGate.register("mcz")
class MCZGate(ControlledGate, FixedGate):
name = "MCZ"

def __init__(self, ctrls, targ: int):
ControlledGate.__init__(self, "Z", ctrls, [targ], None, tar_matrix=ZMatrix)


@QuantumGate.register('ccx')
@QuantumGate.register("ccx")
class ToffoliGate(ControlledGate, FixedGate):
name = "CCX"

def __init__(self, ctrl1: int, ctrl2: int, targ: int):
ControlledGate.__init__(self, "X", [ctrl1, ctrl2], [targ], None, tar_matrix=XMatrix)
ControlledGate.__init__(
self, "X", [ctrl1, ctrl2], [targ], None, tar_matrix=XMatrix
)
45 changes: 26 additions & 19 deletions quafu/elements/element_gates/pauli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@
from quafu.elements.matrices import SWMatrix, WMatrix, XMatrix, YMatrix, ZMatrix
from quafu.elements.quantum_gate import FixedGate, QuantumGate, SingleQubitGate

from quafu.elements.matrices import XMatrix, YMatrix, ZMatrix, WMatrix, SWMatrix
from quafu.elements.quantum_gate import FixedGate, SingleQubitGate, QuantumGate

__all__ = ['IdGate', 'XGate', 'YGate', 'ZGate',
'WGate', 'SWGate', 'SWdgGate',
'SXGate', 'SYGate', 'SXdgGate', 'SYdgGate'] # hint: "SZ" gate is S contained in Clifford gates


@QuantumGate.register('id')
__all__ = [
"IdGate",
"XGate",
"YGate",
"ZGate",
"WGate",
"SWGate",
"SWdgGate",
"SXGate",
"SYGate",
"SXdgGate",
"SYdgGate",
] # hint: "SZ" gate is S contained in Clifford gates


@QuantumGate.register("id")
class IdGate(FixedGate, SingleQubitGate):
name = "Id"
matrix = XMatrix
Expand All @@ -19,7 +26,7 @@ def __init__(self, pos: int):
FixedGate.__init__(self, pos)


@QuantumGate.register('x')
@QuantumGate.register("x")
class XGate(FixedGate, SingleQubitGate):
name = "X"
matrix = XMatrix
Expand All @@ -28,7 +35,7 @@ def __init__(self, pos: int):
FixedGate.__init__(self, pos)


@QuantumGate.register('y')
@QuantumGate.register("y")
class YGate(FixedGate, SingleQubitGate):
name = "Y"
matrix = YMatrix
Expand All @@ -37,7 +44,7 @@ def __init__(self, pos: int):
FixedGate.__init__(self, pos)


@QuantumGate.register('z')
@QuantumGate.register("z")
class ZGate(FixedGate, SingleQubitGate):
name = "Z"
matrix = ZMatrix
Expand All @@ -46,7 +53,7 @@ def __init__(self, pos: int):
FixedGate.__init__(self, pos)


@QuantumGate.register('w')
@QuantumGate.register("w")
class WGate(FixedGate, SingleQubitGate):
"""(X+Y)/sqrt(2)"""

Expand All @@ -65,7 +72,7 @@ def to_qasm(self):
)


@QuantumGate.register('sw')
@QuantumGate.register("sw")
class SWGate(FixedGate, SingleQubitGate):
name = "SW"
matrix = SWMatrix
Expand All @@ -82,7 +89,7 @@ def to_qasm(self):
)


@QuantumGate.register('swdg')
@QuantumGate.register("swdg")
class SWdgGate(FixedGate, SingleQubitGate):
name = "SWdg"
matrix = SWMatrix
Expand All @@ -99,7 +106,7 @@ def to_qasm(self):
)


@QuantumGate.register('sx')
@QuantumGate.register("sx")
class SXGate(FixedGate, SingleQubitGate):
name = "SX"
matrix = np.array(
Expand All @@ -110,7 +117,7 @@ def __init__(self, pos: int):
FixedGate.__init__(self, pos)


@QuantumGate.register('sxdg')
@QuantumGate.register("sxdg")
class SXdgGate(FixedGate, SingleQubitGate):
name = "SXdg"
matrix = SXGate.matrix.conj().T
Expand All @@ -120,7 +127,7 @@ def __init__(self, pos: int):
self.symbol = "√X†"


@QuantumGate.register('sy')
@QuantumGate.register("sy")
class SYGate(FixedGate, SingleQubitGate):
name = "SY"
matrix = np.array(
Expand All @@ -135,7 +142,7 @@ def to_qasm(self):
return "ry(pi/2) q[%d];" % self.pos


@QuantumGate.register('sydg')
@QuantumGate.register("sydg")
class SYdgGate(FixedGate, SingleQubitGate):
name = "SYdg"
matrix = SYGate.matrix.conj().T
Expand Down
9 changes: 3 additions & 6 deletions quafu/elements/element_gates/swap.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
from ..quantum_gate import FixedGate, MultiQubitGate, QuantumGate
from quafu.elements.matrices import ISwapMatrix, SwapMatrix

from typing import Dict

from quafu.elements.matrices import ISwapMatrix, SwapMatrix
Expand All @@ -10,7 +7,7 @@
__all__ = ["ISwapGate", "SwapGate"]


@QuantumGate.register('iswap')
@QuantumGate.register("iswap")
class ISwapGate(FixedGate, MultiQubitGate):
name = "iSWAP"
matrix = ISwapMatrix
Expand All @@ -27,7 +24,7 @@ def named_pos(self) -> Dict:
return {"pos": self.pos}


@QuantumGate.register('swap')
@QuantumGate.register("swap")
class SwapGate(FixedGate, MultiQubitGate):
name = "SWAP"
matrix = SwapMatrix
Expand All @@ -41,4 +38,4 @@ def get_targ_matrix(self, reverse_order=False):

@property
def named_pos(self) -> Dict:
return {'pos': self.pos}
return {"pos": self.pos}
9 changes: 5 additions & 4 deletions quafu/elements/oracle.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,11 @@ def customize_gate(
if cls_name in QuantumGate.gate_classes:
raise ValueError(f"Gate class {cls_name} already exists.")

attrs = {'cls_name': cls_name,
'gate_structure': gate_structure,
'qubit_num': qubit_num,
}
attrs = {
"cls_name": cls_name,
"gate_structure": gate_structure,
"qubit_num": qubit_num,
}

customized_cls = OracleGateMeta(cls_name, (OracleGate,), attrs)
assert issubclass(customized_cls, OracleGate)
Expand Down
Loading

0 comments on commit 3f93d55

Please sign in to comment.