Skip to content

Commit

Permalink
improve readability
Browse files Browse the repository at this point in the history
  • Loading branch information
Nozidoali committed Jun 14, 2024
1 parent 43c9105 commit 1413d83
Show file tree
Hide file tree
Showing 17 changed files with 17 additions and 115 deletions.
2 changes: 1 addition & 1 deletion xyz/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
from .algorithms import *
from .utils import *
from .circuit import *
from .qiskit_utils import *
from .external import *
4 changes: 3 additions & 1 deletion xyz/circuit/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
quantum circuit implementation
------------------------------

Highlights:
Highlights: our implementation of quantum circuits leverages the following features:
- Sparse matrix representation (see `qstate.py`)
- Fast simulation of quantum circuits (see `gate/<gate>.py`)
- On-the-fly gate decomposition (see `gate/decompose.py`)
- On-the-fly redundant gate removal (see `_optimization.py`)
2 changes: 1 addition & 1 deletion xyz/circuit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
from .gate import *
from .qcircuit import *
from .decomposition import *
from .utils import *
from .to_tikz import *
63 changes: 2 additions & 61 deletions xyz/circuit/_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,6 @@


def __map_muxy(gate: MULTIPLEXY) -> List[QGate]:
"""Convert a multi - gate into a list of gates .
:param gate: [description]
:type gate: MULTIPLEXY
:return: [description]
:rtype: List[QGate]
"""
assert gate.type == QGateType.MULTIPLEX_Y
rotation_table = [gate.theta0, gate.theta1]

Expand All @@ -41,14 +34,6 @@ def __map_muxy(gate: MULTIPLEXY) -> List[QGate]:


def __map_mcry(gate: QGate) -> List[QGate]:
"""Convert a MCRY gate into a list of gates .
:param gate: [description]
:type gate: QGate
:raises Exception: [description]
:return: [description]
:rtype: List[QGate]
"""
match gate.get_qgate_type():
case QGateType.MCRY:
control_qubits = gate.control_qubits
Expand Down Expand Up @@ -82,16 +67,9 @@ def __map_mcry(gate: QGate) -> List[QGate]:


def __map_mcmy(gate: MCMY) -> List[QGate]:
"""Convert a MCMY gate into a list of gates .
:param gate: [description]
:type gate: MCMY
:return: [description]
:rtype: List[QGate]
"""
"""Convert a MCMY gate into a list of gates ."""

rotation_table = gate.rotation_table

control_sequence = decompose_mcry(rotation_table=rotation_table)

gates = control_sequence_to_gates(
Expand All @@ -104,11 +82,7 @@ def __map_mcmy(gate: MCMY) -> List[QGate]:


def theta_to_unitary(theta: float):
"""Converts theta to a unitary unitary gate .
:param theta: [description]
:type theta: float
"""
"""Converts theta to a unitary unitary gate ."""
raw_matrix = np.array(
[
[np.cos(theta / 2), -np.sin(theta / 2)],
Expand All @@ -119,17 +93,6 @@ def theta_to_unitary(theta: float):


def unitary_convert(unitary: np.ndarray, coef: float, signal: int):
"""Convert a vector in a unitary unitary function to a unitary function .
:param unitary: [description]
:type unitary: np.ndarray
:param coef: [description]
:type coef: float
:param signal: [description]
:type signal: int
:return: [description]
:rtype: [type]
"""
param = 1 / np.abs(coef)

values, vectors = np.linalg.eig(unitary)
Expand All @@ -148,14 +111,6 @@ def unitary_convert(unitary: np.ndarray, coef: float, signal: int):


def __map_mcry_linear(mcry_gate: MCRY) -> List[QGate]:
"""Map a gate to the circuit .
:param gate: [description]
:type gate: QGate
:return: [description]
:rtype: List[QGate]
"""

assert mcry_gate.get_qgate_type() == QGateType.MCRY

# we first preprocess the relavant qubits
Expand All @@ -178,20 +133,6 @@ def __map_mcry_linear(mcry_gate: MCRY) -> List[QGate]:
def convert_rec(
unitary: np.ndarray, num_qubits: int, is_first: bool = True, step: int = 1
):
"""Convert a unitary unitary to a unitary gate .
:param unitary: [description]
:type unitary: np.ndarray
:param num_qubits: [description]
:type num_qubits: int
:param is_first: [description], defaults to True
:type is_first: bool, optional
:param step: [description], defaults to 1
:type step: int, optional
:return: [description]
:rtype: [type]
"""

nonlocal qubit_list

pairs = namedtuple("pairs", ["control", "target"])
Expand Down
5 changes: 0 additions & 5 deletions xyz/circuit/_optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@


def _add_gate_optimized(self, gate: QGate) -> None:
"""Add a new gate to this gate .
:param gate: [description]
:type gate: QGate
"""
match gate.get_qgate_type():
case QGateType.CU:
if gate.is_z_trivial():
Expand Down
2 changes: 1 addition & 1 deletion xyz/circuit/gate/base/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
Last Modified time: 2023-06-23 00:09:04
"""

from .control import *
from .controlled_gates import *
from .rotation import *
from .qubit import *
from .gate import *
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,15 @@ def __init__(self, control_qubit: QBit, phase: int = 1) -> None:
self.control_qubit = control_qubit

def get_control_qubit(self) -> QBit:
"""Returns the control qubit .
:return: [description]
:rtype: QBit
"""
"""Returns the control qubit ."""
return self.control_qubit

def get_phase(self) -> int:
"""Returns the phase .
:return: [description]
:rtype: int
"""
"""Returns the phase ."""
return self.phase

def is_enabled(self, index: int):
"""Returns True if the control qubit is enabled .
:param index: [description]
:type index: int
:return: [description]
:rtype: bool
"""
"""Returns True if the control qubit is enabled ."""
return (index >> self.control_qubit.index) & 1 == self.phase


Expand All @@ -57,35 +43,19 @@ def __init__(self, control_qubits: List[QBit], phases: List[int]) -> None:
self.phases = list(phases)[:]

def has_zero_controls(self) -> bool:
"""Returns True if any control qubits are zero .
:return: [description]
:rtype: bool
"""
"""Returns True if any control qubits are zero ."""
return len(self.control_qubits) == 0

def has_one_control(self) -> bool:
"""Returns True if this instruction has one control .
:return: [description]
:rtype: bool
"""
"""Returns True if this instruction has one control ."""
return len(self.control_qubits) == 1

def get_control_qubits(self) -> List[QBit]:
"""Returns the control qubits .
:return: [description]
:rtype: List[QBit]
"""
"""Returns the control qubits ."""
return self.control_qubits

def get_phases(self) -> List[int]:
"""Returns the phases .
:return: [description]
:rtype: List[int]
"""
"""Returns the phases ."""
return self.phases

def is_enabled(self, index: int) -> bool:
Expand Down
9 changes: 1 addition & 8 deletions xyz/circuit/gate/base/gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ class QGateType(Enum):
"""This method is used to set the auto - gate type for QGate type .
When defining a new gate, we need to register here to get the type name of the gate .
:param Enum: [description]
:type Enum: [type]
"""

U = auto()
Expand Down Expand Up @@ -72,11 +69,7 @@ def get_qgate_type(self) -> QGateType:


class BasicGate(QGate):
"""Class method for creating a gate .
:param QGate: [description]
:type QGate: [type]
"""
"""Class method for creating a gate ."""

def __init__(self, qgate_type: QGateType, target_qubit: QBit) -> None:
QGate.__init__(self, qgate_type)
Expand Down
1 change: 1 addition & 0 deletions xyz/circuit/qcircuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from .gate import QGate, QGateType, QBit
from ._optimization import _add_gate_optimized, _add_gates_optimized


class QCircuit:
"""the class of quantum circuit"""

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 1413d83

Please sign in to comment.