Skip to content

Commit

Permalink
Quantum circuit interface completed, test needed. Next instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinMTO committed Oct 19, 2023
1 parent ca7107a commit 5703cbb
Show file tree
Hide file tree
Showing 24 changed files with 234 additions and 65 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,4 @@ repos:
rev: v2.2.5
hooks:
- id: codespell
args: [ "-L", "wille,braket,coo", "--skip", "*.ipynb" ]
args: ["-L", "wille,braket,coo", "--skip", "*.ipynb"]
3 changes: 2 additions & 1 deletion src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
measure q[1] -> meas[1];
measure q[2] -> meas[2];
"""

c = QuantumCircuit()
c.from_qasm(qasm)
print(QASM().parse_ditqasm2_str(qasm))
s = QuantumRegister("x", 2)
print(s.__qasm__)
Expand Down
77 changes: 37 additions & 40 deletions src/mqt/circuit/circuit.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from typing import ClassVar

from mqt.circuit.quantum_register import QuantumRegister
Expand Down Expand Up @@ -59,58 +61,70 @@ def append(self, qreg: QuantumRegister):
qreg.local_sitemap[i] = num_lines_stored + i
self._sitemap[(str(qreg.label), i)] = (num_lines_stored + i, qreg.dimensions[i])

def csum(self, control: int, target: int):
def csum(self, qudits: list[int]):
pass
# self.instructions.append(CSum())

def custom_unitary(self):
def custom_unitary(self, qudits: list[int] | int):
pass

def cx(self):
def cx(self, qudits: list[int]):
pass

def h(self):
def h(self, qudit: int):
pass

def ls(self):
def ls(self, qudits: list[int]):
pass

def ms(self):
def ms(self, qudits: list[int]):
pass

def pm(self):
def pm(self, params, qudits: list[int] | int):
pass

def r(self):
def r(self, params, qudit: int):
pass

def rz(self):
def rz(self, params, qudit: int):
pass

def s(self):
def s(self, qudit: int):
pass

def z(self):
def z(self, qudit: int):
pass

def from_qasm_file(self, fname):
def from_qasm(self, qasm_prog):
self.reset()
qasm_parser = QASM().parse_ditqasm2_file(fname)

self._num_qudits = qasm_parser["n"]
qasm_parser = QASM().parse_ditqasm2_str(qasm_prog)
instructions = qasm_parser["instructions"]
self._sitemap = qasm_parser["sitemap"]
# self.number_gates = self._sitemap["n_gates"]
temp_sitemap = qasm_parser["sitemap"]
for qreg in QuantumRegister.from_map(temp_sitemap):
self.append(qreg)

qasm_set = self.get_qasm_set()
for op in instructions:
if op in qasm_set:
if op["name"] in qasm_set:
gate_constructor_name = qasm_set[op["name"]]
if hasattr(self, gate_constructor_name):
function = getattr(self, gate_constructor_name)
function(op["params"], op["qudits"])

tuples_qudits = op["qudits"]
if not tuples_qudits:
msg = "Qudit parameter not applied"
raise IndexError(msg)
# Check if the list contains only one tuple
if len(tuples_qudits) == 1:
qudits_call = tuples_qudits[0][0]
# Extract the first element from each tuple and return as a list
else:
qudits_call = [t[0] for t in list(tuples_qudits)]
if op["params"]:
function(op["params"], qudits_call)
else:
function(qudits_call)
else:
msg = "the require gate is not available anymore."
msg = "the required gate is not available anymore."
raise NotImplementedError(msg)

def to_qasm(self):
Expand All @@ -121,26 +135,9 @@ def to_qasm(self):
for op in self.instructions:
text += op.__qasm__

"""
def draw(self):
custom_counter = 0
for line in self.qreg:
print("|0>---", end="")
for gate in line:
if isinstance(gate, Rz):
print("--[Rz" + str(gate.lev) + "(" + str(round(gate.theta, 2)) + ")]--", end="")
elif isinstance(gate, R):
print("--[R" + str(gate.lev_a) + str(gate.lev_b) + "(" + str(round(gate.theta, 2)) + "," + str(
round(gate.phi, 2)) + ")]--", end="")
elif isinstance(gate, Custom_Unitary):
print("--[C" + str(custom_counter) + "]--", end="")
custom_counter = custom_counter + 1
print("---=||")
"""
# TODO
pass

def reset(self):
self.number_gates = 0
Expand Down
10 changes: 7 additions & 3 deletions src/mqt/circuit/instructions/gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ def to_matrix(self) -> Callable[[str], ndarray]:
def control(
self,
num_ctrl_qudits: int = 1,
label_indeces: list[int] | list[str] | int | str | None = None,
ctrl_state: list[int] | list[str] | int | str | None = None,
label_indeces: list[int] | int | None = None,
ctrl_state: list[int] | int | None = None,
):
pass
"""Return controlled version of gate. See :class:`.ControlledGate` for usage.
Expand Down Expand Up @@ -106,5 +106,9 @@ def validate_parameter(self, parameter) -> bool:

@abstractmethod
def __qasm__(self) -> str:
# TODO
pass

@abstractmethod
def __str__(self):
# String representation for drawing?
pass
12 changes: 11 additions & 1 deletion src/mqt/circuit/instructions/gate_set/csum.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from typing import Any

from mqt.circuit.instructions.gate import Gate
Expand All @@ -10,11 +12,19 @@ def __qasm__(self) -> str:
def __array__(self, dtype: str = "complex") -> Any:
pass

def control(self, num_ctrl_qudits: int = 1, label: str | None = None, ctrl_state: int | str | None = None):
def control(
self,
num_ctrl_qudits: int = 1,
label_indeces: list[int] | int | None = None,
ctrl_state: list[int] | int | None = None,
):
pass

def validate_parameter(self, parameter):
pass

def __init__(self, name: str, num_qudits: int, params: list):
super().__init__(name, num_qudits, params)

def __str__(self):
pass
12 changes: 11 additions & 1 deletion src/mqt/circuit/instructions/gate_set/customunitary.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from typing import Any

from mqt.circuit.instructions.gate import Gate
Expand All @@ -10,11 +12,19 @@ def __qasm__(self) -> str:
def __array__(self, dtype: str = "complex") -> Any:
pass

def control(self, num_ctrl_qudits: int = 1, label: str | None = None, ctrl_state: int | str | None = None):
def control(
self,
num_ctrl_qudits: int = 1,
label_indeces: list[int] | int | None = None,
ctrl_state: list[int] | int | None = None,
):
pass

def validate_parameter(self, parameter):
pass

def __init__(self, name: str, num_qudits: int, params: list):
super().__init__(name, num_qudits, params)

def __str__(self):
pass
12 changes: 11 additions & 1 deletion src/mqt/circuit/instructions/gate_set/cx.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from typing import Any

from mqt.circuit.instructions.gate import Gate
Expand All @@ -10,11 +12,19 @@ def __qasm__(self) -> str:
def __array__(self, dtype: str = "complex") -> Any:
pass

def control(self, num_ctrl_qudits: int = 1, label: str | None = None, ctrl_state: int | str | None = None):
def control(
self,
num_ctrl_qudits: int = 1,
label_indeces: list[int] | int | None = None,
ctrl_state: list[int] | int | None = None,
):
pass

def validate_parameter(self, parameter):
pass

def __init__(self, name: str, num_qudits: int, params: list):
super().__init__(name, num_qudits, params)

def __str__(self):
pass
File renamed without changes.
12 changes: 11 additions & 1 deletion src/mqt/circuit/instructions/gate_set/gellman.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from typing import Any

from mqt.circuit.instructions.gate import Gate
Expand All @@ -10,11 +12,19 @@ def __qasm__(self) -> str:
def __array__(self, dtype: str = "complex") -> Any:
pass

def control(self, num_ctrl_qudits: int = 1, label: str | None = None, ctrl_state: int | str | None = None):
def control(
self,
num_ctrl_qudits: int = 1,
label_indeces: list[int] | int | None = None,
ctrl_state: list[int] | int | None = None,
):
pass

def validate_parameter(self, parameter):
pass

def __init__(self, name: str, num_qudits: int, params: list):
super().__init__(name, num_qudits, params)

def __str__(self):
pass
12 changes: 11 additions & 1 deletion src/mqt/circuit/instructions/gate_set/h.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from typing import Any

from mqt.circuit.instructions.gate import Gate
Expand All @@ -10,11 +12,19 @@ def __qasm__(self) -> str:
def __array__(self, dtype: str = "complex") -> Any:
pass

def control(self, num_ctrl_qudits: int = 1, label: str | None = None, ctrl_state: int | str | None = None):
def control(
self,
num_ctrl_qudits: int = 1,
label_indeces: list[int] | int | None = None,
ctrl_state: list[int] | int | None = None,
):
pass

def validate_parameter(self, parameter):
pass

def __init__(self, name: str, num_qudits: int, params: list):
super().__init__(name, num_qudits, params)

def __str__(self):
pass
12 changes: 11 additions & 1 deletion src/mqt/circuit/instructions/gate_set/ls.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from typing import Any

from mqt.circuit.instructions.gate import Gate
Expand All @@ -10,11 +12,19 @@ def __qasm__(self) -> str:
def __array__(self, dtype: str = "complex") -> Any:
pass

def control(self, num_ctrl_qudits: int = 1, label: str | None = None, ctrl_state: int | str | None = None):
def control(
self,
num_ctrl_qudits: int = 1,
label_indeces: list[int] | int | None = None,
ctrl_state: list[int] | int | None = None,
):
pass

def validate_parameter(self, parameter):
pass

def __init__(self, name: str, num_qudits: int, params: list):
super().__init__(name, num_qudits, params)

def __str__(self):
pass
12 changes: 11 additions & 1 deletion src/mqt/circuit/instructions/gate_set/ms.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from typing import Any

from mqt.circuit.instructions.gate import Gate
Expand All @@ -10,11 +12,19 @@ def __qasm__(self) -> str:
def __array__(self, dtype: str = "complex") -> Any:
pass

def control(self, num_ctrl_qudits: int = 1, label: str | None = None, ctrl_state: int | str | None = None):
def control(
self,
num_ctrl_qudits: int = 1,
label_indeces: list[int] | int | None = None,
ctrl_state: list[int] | int | None = None,
):
pass

def validate_parameter(self, parameter):
pass

def __init__(self, name: str, num_qudits: int, params: list):
super().__init__(name, num_qudits, params)

def __str__(self):
pass
12 changes: 11 additions & 1 deletion src/mqt/circuit/instructions/gate_set/perm.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from typing import Any

from mqt.circuit.instructions.gate import Gate
Expand All @@ -10,11 +12,19 @@ def __qasm__(self) -> str:
def __array__(self, dtype: str = "complex") -> Any:
pass

def control(self, num_ctrl_qudits: int = 1, label: str | None = None, ctrl_state: int | str | None = None):
def control(
self,
num_ctrl_qudits: int = 1,
label_indeces: list[int] | int | None = None,
ctrl_state: list[int] | int | None = None,
):
pass

def validate_parameter(self, parameter):
pass

def __init__(self, name: str, num_qudits: int, params: list):
super().__init__(name, num_qudits, params)

def __str__(self):
pass
Loading

0 comments on commit 5703cbb

Please sign in to comment.