-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
completed simulation of simple two-qutrit circuit with one hadamard
- Loading branch information
Showing
64 changed files
with
1,077 additions
and
608 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
from __future__ import annotations | ||
|
||
from abc import ABC, abstractmethod | ||
from typing import TYPE_CHECKING, Callable | ||
|
||
from mqt.circuit.components.instructions.gate_extensions.controls import ControlData | ||
from mqt.circuit.components.instructions.gate_extensions.gatetypes import GateTypes | ||
from mqt.circuit.components.instructions.instruction import Instruction | ||
from mqt.exceptions.circuiterror import CircuitError | ||
|
||
if TYPE_CHECKING: | ||
import enum | ||
|
||
import numpy as np | ||
from numpy import ndarray | ||
|
||
from mqt.circuit.circuit import QuantumCircuit | ||
|
||
|
||
class Gate(Instruction, ABC): | ||
"""Unitary gate.""" | ||
|
||
def __init__( | ||
self, | ||
circuit: QuantumCircuit, | ||
name: str, | ||
gate_type: enum, | ||
target_qudits: list[int] | int, | ||
dimensions: list[int] | int, | ||
params: list | None = None, | ||
control_set=None, | ||
label: str | None = None, | ||
duration=None, | ||
unit="dt", | ||
) -> None: | ||
self.parent_circuit = circuit | ||
# self.definition = None #todo unsure whether necesssary | ||
self._name = name | ||
self._target_qudits = target_qudits | ||
self._dimensions = dimensions | ||
self._params = params | ||
self._label = label | ||
self._duration = duration | ||
self._unit = unit | ||
self._controls_data = None | ||
if control_set: | ||
self.control(**vars(control_set)) | ||
# TODO do it with inheritance one day | ||
self.gate_type = gate_type | ||
|
||
# Set higher priority than Numpy array and matrix classes | ||
__array_priority__ = 20 | ||
|
||
@property | ||
def ref_lines(self): | ||
if isinstance(self._target_qudits, int): | ||
lines = self.get_control_lines | ||
lines.append(self._target_qudits) | ||
elif isinstance(self._target_qudits, list): | ||
lines = self._target_qudits + self.get_control_lines | ||
if len(lines) == 0: | ||
msg = "Gate has no target or control lines" | ||
raise CircuitError(msg) | ||
return lines | ||
|
||
@abstractmethod | ||
def __array__(self, dtype: str = "complex") -> np.ndarray: | ||
pass | ||
|
||
def to_matrix(self) -> Callable[[str], ndarray]: | ||
"""Return a np.ndarray for the gate unitary matrix. | ||
Returns: | ||
np.ndarray: if the Gate subclass has a matrix definition. | ||
Raises: | ||
CircuitError: If a Gate subclass does not implement this method an | ||
exception will be raised when this base class method is called. | ||
""" | ||
if hasattr(self, "__array__"): | ||
return self.__array__() | ||
msg = "to_matrix not defined for this " | ||
raise CircuitError(msg, {type(self)}) | ||
|
||
def control(self, indices: list[int] | int, ctrl_states: list[int] | int): | ||
if len(indices) > self.parent_circuit.num_qudits or any( | ||
idx >= self.parent_circuit.num_qudits for idx in indices | ||
): | ||
msg = "Indices or Number of Controls is beyond the Quantum Circuit Size" | ||
raise IndexError(msg) | ||
if any(idx in self._target_qudits for idx in indices): | ||
msg = "Controls overlap with targets" | ||
raise IndexError(msg) | ||
if any(ctrl >= self._dimensions[i] for i, ctrl in enumerate(ctrl_states)): | ||
msg = "Controls States beyond qudit size " | ||
raise IndexError(msg) | ||
if self.ref_lines < 2: | ||
self.set_gate_type_single() | ||
elif self.ref_lines == 2: | ||
self.set_gate_type_two() | ||
elif self.ref_lines > 2: | ||
self.set_gate_type_multi() | ||
self._controls_data = ControlData(indices, ctrl_states) | ||
|
||
@abstractmethod | ||
def validate_parameter(self, parameter): | ||
pass | ||
|
||
@abstractmethod | ||
def __qasm__(self) -> str: | ||
pass | ||
|
||
@abstractmethod | ||
def __str__(self): | ||
# String representation for drawing? | ||
pass | ||
|
||
def set_gate_type_single(self): | ||
self.gate_type = GateTypes.SINGLE | ||
|
||
def set_gate_type_two(self): | ||
self.gate_type = GateTypes.TWO | ||
|
||
def set_gate_type_multi(self): | ||
self.gate_type = GateTypes.MULTI | ||
|
||
@property | ||
def get_control_lines(self): | ||
if self._controls_data: | ||
return self._controls_data.indices | ||
return [] |
File renamed without changes.
9 changes: 9 additions & 0 deletions
9
src/mqt/circuit/components/instructions/gate_extensions/controls.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from __future__ import annotations | ||
|
||
from dataclasses import dataclass | ||
|
||
|
||
@dataclass | ||
class ControlData: | ||
indices: list[int] | int | ||
ctrl_states: list[int] | int |
12 changes: 12 additions & 0 deletions
12
src/mqt/circuit/components/instructions/gate_extensions/gatetypes.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import enum | ||
|
||
|
||
class GateTypes(enum.Enum): | ||
"""Enumeration for job status.""" | ||
|
||
SINGLE = "Single Qudit Gate" | ||
TWO = "Two Qudit Gate" | ||
MULTI = "Multi Qudit Gate" | ||
|
||
|
||
CORE_GATE_TYPES = (GateTypes.SINGLE, GateTypes.TWO, GateTypes.MULTI) |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
from mqt.circuit.components.instructions.gate import Gate | ||
|
||
if TYPE_CHECKING: | ||
import numpy as np | ||
|
||
from mqt.circuit.components.instructions.gate_extensions.controls import ControlData | ||
|
||
|
||
class CSum(Gate): | ||
def __qasm__(self) -> str: | ||
pass | ||
|
||
def __array__(self, dtype: str = "complex") -> np.ndarray: | ||
pass | ||
|
||
def validate_parameter(self, parameter): | ||
pass | ||
|
||
def __init__(self, name: str, num_qudits: int, params: list, controls: ControlData | None = None): | ||
super().__init__(name, num_qudits, params) | ||
|
||
def __str__(self): | ||
pass |
27 changes: 27 additions & 0 deletions
27
src/mqt/circuit/components/instructions/gate_set/customunitary.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
from mqt.circuit.components.instructions.gate import Gate | ||
|
||
if TYPE_CHECKING: | ||
import numpy as np | ||
|
||
from mqt.circuit.components.instructions.gate_extensions.controls import ControlData | ||
|
||
|
||
class CustomUnitary(Gate): | ||
def __qasm__(self) -> str: | ||
pass | ||
|
||
def __array__(self, dtype: str = "complex") -> np.ndarray: | ||
pass | ||
|
||
def validate_parameter(self, parameter): | ||
pass | ||
|
||
def __init__(self, name: str, num_qudits: int, params: list, controls: ControlData | None = None): | ||
super().__init__(name, num_qudits, params) | ||
|
||
def __str__(self): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
from mqt.circuit.components.instructions.gate import Gate | ||
|
||
if TYPE_CHECKING: | ||
import numpy as np | ||
|
||
from mqt.circuit.components.instructions.gate_extensions.controls import ControlData | ||
|
||
|
||
class CEx(Gate): | ||
def __qasm__(self) -> str: | ||
pass | ||
|
||
def __array__(self, dtype: str = "complex") -> np.ndarray: | ||
pass | ||
|
||
def validate_parameter(self, parameter): | ||
pass | ||
|
||
def __init__(self, name: str, num_qudits: int, params: list, controls: ControlData | None = None): | ||
super().__init__(name, num_qudits, params) | ||
|
||
def __str__(self): | ||
pass |
Oops, something went wrong.