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

Enhance gate register mechanism, add visualization of Bloch Sphere #125

Merged
merged 5 commits into from
Dec 17, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 7 additions & 9 deletions quafu/elements/element_gates/c11.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from ..quantum_gate import ControlledGate, FixedGate
from ..quantum_gate import ControlledGate, FixedGate, QuantumGate
from abc import ABC
from quafu.elements.matrices import XMatrix, YMatrix, ZMatrix, SMatrix, TMatrix, pmatrix
from typing import Dict
Expand All @@ -13,6 +13,7 @@ class _C11Gate(ControlledGate, ABC):
ct_dims = (1, 1, 2)


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

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


@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')
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')
class CSGate(_C11Gate, FixedGate):
name = "CS"

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


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

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


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

Expand All @@ -64,11 +70,3 @@ def __init__(self, ctrl: int, targ: int, paras):
@property
def named_paras(self) -> Dict:
return {'theta': self.paras}


ControlledGate.register_gate(CXGate)
ControlledGate.register_gate(CYGate)
ControlledGate.register_gate(CZGate)
ControlledGate.register_gate(CSGate)
ControlledGate.register_gate(CTGate)
ControlledGate.register_gate(CPGate)
6 changes: 2 additions & 4 deletions quafu/elements/element_gates/c12.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
from ..quantum_gate import ControlledGate, FixedGate
from ..quantum_gate import ControlledGate, FixedGate, QuantumGate
from quafu.elements.matrices import SwapMatrix


@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.register_gate(FredkinGate)
14 changes: 6 additions & 8 deletions quafu/elements/element_gates/clifford.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import numpy as np

from quafu.elements.matrices import HMatrix, SMatrix
from ..quantum_gate import SingleQubitGate, FixedGate
from ..quantum_gate import SingleQubitGate, FixedGate, QuantumGate

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


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


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


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


@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 @@ -38,16 +42,10 @@ def __init__(self, pos: int):
FixedGate.__init__(self, pos)


@QuantumGate.register('tdg')
class TdgGate(SingleQubitGate, FixedGate):
name = "Tdg"
matrix = TGate.matrix.conj().T

def __init__(self, pos: int):
FixedGate.__init__(self, pos)


FixedGate.register_gate(HGate)
FixedGate.register_gate(SGate)
FixedGate.register_gate(SdgGate)
FixedGate.register_gate(TGate)
FixedGate.register_gate(TdgGate)
31 changes: 5 additions & 26 deletions quafu/elements/element_gates/cm1.py
Original file line number Diff line number Diff line change
@@ -1,57 +1,36 @@
from ..quantum_gate import ControlledGate, FixedGate
from ..quantum_gate import ControlledGate, FixedGate, QuantumGate
from quafu.elements.matrices import XMatrix, YMatrix, ZMatrix

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


@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')
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')
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')
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.register_gate(MCXGate)
ControlledGate.register_gate(MCYGate)
ControlledGate.register_gate(MCZGate)
ControlledGate.register_gate(ToffoliGate)

# deprecated

# class ControlledU(ControlledGate):
# """ Controlled gate class, where the matrix act non-trivially on target qubits"""
# name = 'CU'
#
# def __init__(self, ctrls: List[int], u: Union[SingleQubitGate, MultiQubitGate]):
# self.targ_gate = u
# targs = u.pos
# if isinstance(targs, int):
# targs = [targs]
#
# ControlledGate.__init__(self, u.name, ctrls, targs, u.paras, tar_matrix=self.targ_gate.get_targ_matrix())
#
# def get_targ_matrix(self, reverse_order=False):
# return self.targ_gate.get_targ_matrix(reverse_order)
# ControlledGate.register_gate(ControlledU)
35 changes: 23 additions & 12 deletions quafu/elements/element_gates/pauli.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import numpy as np

from quafu.elements.matrices import XMatrix, YMatrix, ZMatrix, WMatrix, SWMatrix
from quafu.elements.quantum_gate import FixedGate, SingleQubitGate
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')
class IdGate(FixedGate, SingleQubitGate):
name = "Id"
matrix = XMatrix
Expand All @@ -16,6 +17,7 @@ def __init__(self, pos: int):
FixedGate.__init__(self, pos)


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


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


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


@QuantumGate.register('w')
class WGate(FixedGate, SingleQubitGate):
""" (X+Y)/sqrt(2) """
name = "W"
Expand All @@ -57,6 +62,7 @@ def to_qasm(self):
)


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


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


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


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


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


@QuantumGate.register('sydg')
class SYdgGate(FixedGate, SingleQubitGate):
name = "SYdg"
matrix = SYGate.matrix.conj().T
Expand All @@ -134,14 +145,14 @@ def to_qasm(self):
return "ry(-pi/2) q[%d]" % self.pos


SingleQubitGate.register_gate(IdGate)
SingleQubitGate.register_gate(XGate)
SingleQubitGate.register_gate(YGate)
SingleQubitGate.register_gate(ZGate)
SingleQubitGate.register_gate(WGate)
SingleQubitGate.register_gate(SWGate)
SingleQubitGate.register_gate(SWdgGate)
SingleQubitGate.register_gate(SXGate)
SingleQubitGate.register_gate(SXdgGate)
SingleQubitGate.register_gate(SYGate)
SingleQubitGate.register_gate(SYdgGate)
# SingleQubitGate.register_gate(IdGate)
# SingleQubitGate.register_gate(XGate)
# SingleQubitGate.register_gate(YGate)
# SingleQubitGate.register_gate(ZGate)
# SingleQubitGate.register_gate(WGate)
# SingleQubitGate.register_gate(SWGate)
# SingleQubitGate.register_gate(SWdgGate)
# SingleQubitGate.register_gate(SXGate)
# SingleQubitGate.register_gate(SXdgGate)
# SingleQubitGate.register_gate(SYGate)
# SingleQubitGate.register_gate(SYdgGate)
8 changes: 3 additions & 5 deletions quafu/elements/element_gates/swap.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from ..quantum_gate import FixedGate, MultiQubitGate
from ..quantum_gate import FixedGate, MultiQubitGate, QuantumGate
from quafu.elements.matrices import ISwapMatrix, SwapMatrix

from typing import Dict

__all__ = ['ISwapGate', 'SwapGate']


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


@QuantumGate.register('swap')
class SwapGate(FixedGate, MultiQubitGate):
name = "SWAP"
matrix = SwapMatrix
Expand All @@ -36,7 +38,3 @@ def get_targ_matrix(self, reverse_order=False):
@property
def named_pos(self) -> Dict:
return {'pos': self.pos}


FixedGate.register_gate(ISwapGate)
FixedGate.register_gate(SwapGate)
2 changes: 1 addition & 1 deletion quafu/elements/oracle.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def customize_gate(cls_name: str,
raise ValueError(f"Gate class {cls_name} already exists.")

attrs = {'cls_name': cls_name,
'gate_structure': gate_structure, # TODO: translate
'gate_structure': gate_structure,
'qubit_num': qubit_num,
}

Expand Down
Loading
Loading