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

Stable/0.3: Further unify interface of Instructioin #96

Merged
merged 7 commits into from
Oct 16, 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
162 changes: 99 additions & 63 deletions src/quafu/circuits/quantum_circuit.py

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions src/quafu/elements/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .instruction import Instruction, Barrier, Measure
from .pulses import Delay, XYResonance, QuantumPulse
from .quantum_gate import QuantumGate, ControlledGate, MultiQubitGate, SingleQubitGate
9 changes: 3 additions & 6 deletions src/quafu/elements/element_gates/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@
from .rotation import RXGate, RYGate, RZGate, RXXGate, RYYGate, RZZGate
from .swap import SwapGate, ISwapGate
from .c11 import CXGate, CYGate, CZGate, CSGate, CTGate, CPGate
from .c21 import ToffoliGate
from .c12 import FredkinGate
from .cm1 import MCXGate, MCYGate, MCZGate, ControlledU
from .cm1 import MCXGate, MCYGate, MCZGate, ToffoliGate
from .unitary import UnitaryDecomposer

__all__ = ['XGate', 'YGate', 'ZGate', 'IdGate', 'WGate', 'SWGate',
'PhaseGate',
'RXGate', 'RYGate', 'RZGate', 'RXXGate', 'RYYGate', 'RZZGate',
'SwapGate', 'ISwapGate',
'SwapGate', 'ISwapGate', 'FredkinGate',
'CXGate', 'CYGate', 'CZGate', 'CSGate', 'CTGate', 'CPGate',
'ToffoliGate',
'FredkinGate',
'MCXGate', 'MCYGate', 'MCZGate', 'ControlledU',
'MCXGate', 'MCYGate', 'MCZGate', 'ToffoliGate',
'UnitaryDecomposer']
36 changes: 23 additions & 13 deletions src/quafu/elements/element_gates/c11.py
Original file line number Diff line number Diff line change
@@ -1,49 +1,55 @@
from ..quantum_element import ControlledGate
from ..quantum_gate import ControlledGate, FixedGate
from abc import ABC
from .matrices import XMatrix, YMatrix, ZMatrix, SMatrix, TMatrix, pmatrix
from quafu.elements.matrices import XMatrix, YMatrix, ZMatrix, SMatrix, TMatrix, pmatrix
from typing import Dict


__all__ = ['CXGate', 'CYGate', 'CZGate',
'CSGate', 'CTGate',
'CPGate']


class _C11Gate(ControlledGate, ABC):
ct_dims = (1, 1, 2)


class CXGate(_C11Gate):
class CXGate(_C11Gate, FixedGate):
name = "CX"

def __init__(self, ctrl: int, targ: int):
super().__init__("X", [ctrl], [targ], None, tar_matrix=XMatrix)
_C11Gate.__init__(self, "X", [ctrl], [targ], None, tar_matrix=XMatrix)
self.symbol = "+"


class CYGate(_C11Gate):
class CYGate(_C11Gate, FixedGate):
name = "CY"

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


class CZGate(_C11Gate):
class CZGate(_C11Gate, FixedGate):
name = "CZ"

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


class CSGate(_C11Gate):
class CSGate(_C11Gate, FixedGate):
name = "CS"

def __init__(self, ctrl: int, targ: int):
super().__init__("S", [ctrl], [targ], None, tar_matrix=SMatrix)
_C11Gate.__init__(self, "S", [ctrl], [targ], None, tar_matrix=SMatrix)

def to_qasm(self):
return "cp(pi/2) " + "q[%d],q[%d]" % (self.pos[0], self.pos[1])


class CTGate(_C11Gate):
class CTGate(_C11Gate, FixedGate):
name = "CT"

def __init__(self, ctrl: int, targ: int):
super().__init__("T", [ctrl], [targ], None, tar_matrix=TMatrix)
_C11Gate.__init__(self, "T", [ctrl], [targ], None, tar_matrix=TMatrix)

def to_qasm(self):
return "cp(pi/4) " + "q[%d],q[%d]" % (self.pos[0], self.pos[1])
Expand All @@ -53,7 +59,11 @@ class CPGate(_C11Gate):
name = "CP"

def __init__(self, ctrl: int, targ: int, paras):
super().__init__("P", [ctrl], [targ], paras, tar_matrix=pmatrix(paras))
_C11Gate.__init__(self, "P", [ctrl], [targ], paras, tar_matrix=pmatrix(paras))

@property
def named_paras(self) -> Dict:
return {'theta': self.paras}


ControlledGate.register_gate(CXGate)
Expand Down
8 changes: 4 additions & 4 deletions src/quafu/elements/element_gates/c12.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from ..quantum_element import ControlledGate
from .matrices import SwapMatrix
from ..quantum_gate import ControlledGate, FixedGate
from quafu.elements.matrices import SwapMatrix


class FredkinGate(ControlledGate):
class FredkinGate(ControlledGate, FixedGate):
name = "CSWAP"

def __init__(self, ctrl: int, targ1: int, targ2: int):
super().__init__("SWAP", [ctrl], [targ1, targ2], None, tar_matrix=SwapMatrix)
ControlledGate.__init__(self, "SWAP", [ctrl], [targ1, targ2], None, tar_matrix=SwapMatrix)


ControlledGate.register_gate(FredkinGate)
13 changes: 0 additions & 13 deletions src/quafu/elements/element_gates/c21.py

This file was deleted.

41 changes: 21 additions & 20 deletions src/quafu/elements/element_gates/clifford.py
Original file line number Diff line number Diff line change
@@ -1,53 +1,54 @@
import numpy as np

from quafu.elements.element_gates.matrices import HMatrix
from quafu.elements.quantum_element import FixedSingleQubitGate
from quafu.elements.matrices import HMatrix, SMatrix
from ..quantum_gate import SingleQubitGate, FixedGate

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

class HGate(FixedSingleQubitGate):

class HGate(SingleQubitGate, FixedGate):
name = "H"
matrix = HMatrix

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


class SGate(FixedSingleQubitGate):
class SGate(SingleQubitGate, FixedGate):
name = "S"
matrix = np.array([[1., 0.],
[0., 1.j]], dtype=complex)
matrix = SMatrix

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


class SdgGate(FixedSingleQubitGate):
class SdgGate(SingleQubitGate, FixedGate):
name = "Sdg"
matrix = SGate.matrix.conj().T
matrix = SMatrix.conj().T

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


class TGate(FixedSingleQubitGate):
class TGate(SingleQubitGate, FixedGate):
name = "T"
matrix = np.array([[1., 0.],
[0., np.exp(1.j * np.pi / 4)]], dtype=complex)

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


class TdgGate(FixedSingleQubitGate):
class TdgGate(SingleQubitGate, FixedGate):
name = "Tdg"
matrix = TGate.matrix.conj().T

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


FixedSingleQubitGate.register_gate(HGate)
FixedSingleQubitGate.register_gate(SGate)
FixedSingleQubitGate.register_gate(SdgGate)
FixedSingleQubitGate.register_gate(TGate)
FixedSingleQubitGate.register_gate(TdgGate)
FixedGate.register_gate(HGate)
FixedGate.register_gate(SGate)
FixedGate.register_gate(SdgGate)
FixedGate.register_gate(TGate)
FixedGate.register_gate(TdgGate)
55 changes: 32 additions & 23 deletions src/quafu/elements/element_gates/cm1.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,56 @@
from typing import List, Union
from ..quantum_gate import ControlledGate, FixedGate
from quafu.elements.matrices import XMatrix, YMatrix, ZMatrix

from .matrices import XMatrix, YMatrix, ZMatrix
from ..quantum_element import ControlledGate, SingleQubitGate, MultiQubitGate
__all__ = ['MCXGate', 'MCYGate', 'MCZGate', 'ToffoliGate']


class MCXGate(ControlledGate):
class MCXGate(ControlledGate, FixedGate):
name = "MCX"

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


class MCYGate(ControlledGate):
class MCYGate(ControlledGate, FixedGate):
name = "MCY"

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


class MCZGate(ControlledGate):
class MCZGate(ControlledGate, FixedGate):
name = "MCZ"

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


class ControlledU(ControlledGate):
""" Controlled gate class, where the matrix act non-trivially on target qubits"""
name = 'CU'
class ToffoliGate(ControlledGate, FixedGate):
name = "CCX"

def __init__(self, ctrls: List[int], u: Union[SingleQubitGate, MultiQubitGate]):
self.targ_gate = u
targs = u.pos
if isinstance(targs, int):
targs = [targs]

super().__init__(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)
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(ControlledU)
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)
Loading