-
Notifications
You must be signed in to change notification settings - Fork 6
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
Feat db jobs and results #118
base: dev
Are you sure you want to change the base?
Changes from 25 commits
78dd518
9b997eb
55f0436
e2d618c
4dcce28
c0a7178
77cf4c7
60407bf
def8396
6292d22
d37304f
2f4dec1
4e4a083
b2f88da
3edf6c0
6f7820f
a3dfc9d
1c8e6c2
21608ec
01a5597
a3431e1
ff141c5
066fabd
a2ad91c
192bae0
1db6b08
c0f4390
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,8 +35,7 @@ | |
|
||
from copy import deepcopy | ||
from numbers import Complex | ||
from pickle import dumps | ||
from typing import TYPE_CHECKING, Iterable, Optional, Sequence, Type | ||
from typing import TYPE_CHECKING, Optional, Sequence, Type | ||
from warnings import warn | ||
|
||
import numpy as np | ||
|
@@ -159,13 +158,18 @@ def __init__( | |
connections: set[int] = set.union( | ||
*(instruction.connections() for instruction in data) | ||
) | ||
self._nb_qubits = max(connections) + 1 | ||
self._nb_qubits = ( | ||
max(connections) + 1 if len(connections) != 0 else 0 | ||
) | ||
else: | ||
self._nb_qubits = nb_qubits | ||
self.add(deepcopy(data)) | ||
|
||
def __eq__(self, value: object) -> bool: | ||
return dumps(self) == dumps(value) | ||
if not isinstance(value, QCircuit): | ||
return False | ||
|
||
return self.to_dict() == value.to_dict() | ||
|
||
def add(self, components: OneOrMany[Instruction | NoiseModel]): | ||
"""Adds a ``component`` or a list of ``component`` at the end of the | ||
|
@@ -205,7 +209,7 @@ def add(self, components: OneOrMany[Instruction | NoiseModel]): | |
|
||
""" | ||
|
||
if isinstance(components, Iterable): | ||
if not isinstance(components, (Instruction, NoiseModel)): | ||
for comp in components: | ||
self.add(comp) | ||
return | ||
|
@@ -231,7 +235,11 @@ def add(self, components: OneOrMany[Instruction | NoiseModel]): | |
components.c_targets = [ | ||
self.nb_cbits + i for i in range(len(components.targets)) | ||
] | ||
self.nb_cbits = max(self.nb_cbits, max(components.c_targets) + 1) | ||
self.nb_cbits = ( | ||
max(self.nb_cbits, max(components.c_targets) + 1) | ||
if len(components.c_targets) != 0 | ||
else 0 | ||
) | ||
|
||
if isinstance(components, NoiseModel): | ||
self.noises.append(components) | ||
|
@@ -318,12 +326,12 @@ def _update_targets_components(self, component: Instruction | NoiseModel): | |
|
||
if self.nb_cbits is None: | ||
self.nb_cbits = 0 | ||
unique_cbits = set() | ||
unique_cbits: set[int] = set() | ||
for instruction in self.instructions: | ||
if instruction != component and isinstance(instruction, BasisMeasure): | ||
if instruction.c_targets: | ||
unique_cbits.update(instruction.c_targets) | ||
c_targets = [] | ||
c_targets: list[int] = [] | ||
i = 0 | ||
for _ in range(len(component.targets)): | ||
while i in unique_cbits: | ||
|
@@ -430,11 +438,26 @@ def append(self, other: QCircuit, qubits_offset: int = 0) -> None: | |
if isinstance(inst, ControlledGate): | ||
inst.controls = [qubit + qubits_offset for qubit in inst.controls] | ||
if isinstance(inst, BasisMeasure): | ||
if not inst.user_set_c_targets: | ||
if not inst._user_set_c_targets: # pyright: ignore[reportPrivateUsage] | ||
inst.c_targets = None | ||
|
||
self.add(inst) | ||
|
||
def to_dict(self) -> dict[str, int | str | list[str] | float | None]: | ||
Henri-ColibrITD marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
Serialize the quantum circuit to a dictionary. | ||
Returns: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you think we can include a small example for this method in the docstring? |
||
dict: A dictionary representation of the circuit. | ||
""" | ||
|
||
return { | ||
attr_name: getattr(self, attr_name) | ||
for attr_name in dir(self) | ||
if attr_name not in {'_nb_qubits', 'gates', 'measurements', 'breakpoints'} | ||
and not attr_name.startswith("__") | ||
and not callable(getattr(self, attr_name)) | ||
} | ||
|
||
def __iadd__(self, other: QCircuit): | ||
self.append(other) | ||
return self | ||
|
@@ -1280,12 +1303,14 @@ def __str__(self) -> str: | |
|
||
def __repr__(self) -> str: | ||
instructions_repr = ", ".join(repr(instr) for instr in self.instructions) | ||
|
||
if self.noises: | ||
noise_repr = ", ".join(map(repr, self.noises)) | ||
return f'QCircuit([{instructions_repr}, {noise_repr}], nb_qubits={self.nb_qubits}, nb_cbits={self.nb_cbits}, label="{self.label}")' | ||
label = f', label="{self.label}"' if self.label is not None else "" | ||
nb_cbits = f", nb_cbits={self.nb_cbits}" if self.nb_cbits is not None else "" | ||
if instructions_repr == "": | ||
noise = ", " + ", ".join(map(repr, self.noises)) if self.noises else "" | ||
else: | ||
return f'QCircuit([{instructions_repr}], nb_qubits={self.nb_qubits}, nb_cbits={self.nb_cbits}, label="{self.label}")' | ||
noise = ", ".join(map(repr, self.noises)) if self.noises else "" | ||
|
||
return f'QCircuit([{instructions_repr}{noise}], nb_qubits={self.nb_qubits}{nb_cbits}{label})' | ||
|
||
def variables(self) -> set[Basic]: | ||
"""Returns all the symbolic parameters involved in this circuit. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -104,7 +104,7 @@ def __str__(self) -> str: | |
from mpqp.core.circuit import QCircuit | ||
|
||
connection = self.connections() | ||
circuit_size = max(connection) + 1 if connection else 1 | ||
circuit_size = max(connection) + 1 if len(connection) == 0 else 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You mean |
||
circuit = QCircuit(circuit_size) | ||
circuit.add(self) | ||
return str(circuit) | ||
|
@@ -115,6 +115,17 @@ def __repr__(self) -> str: | |
controls = str(self.controls) + "," if isinstance(self, ControlledGate) else "" | ||
return f"{type(self).__name__}({controls}{self.targets})" | ||
|
||
def to_dict(self): | ||
return { | ||
attr_name: getattr(self, attr_name) | ||
for attr_name in dir(self) | ||
if ( | ||
not attr_name.startswith("_abc_") | ||
and not attr_name.startswith("__") | ||
and not callable(getattr(self, attr_name)) | ||
) | ||
} | ||
|
||
def connections(self) -> set[int]: | ||
"""Returns the indices of the qubits connected to the instruction. | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -163,6 +163,17 @@ def to_computational(self): | |
] | ||
) | ||
|
||
def __eq__(self, other: object) -> bool: | ||
Henri-ColibrITD marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you take into account here that we can call |
||
if not isinstance(other, Basis): | ||
return False | ||
|
||
return ( | ||
self.nb_qubits == other.nb_qubits | ||
and np.array_equal(self.basis_vectors, other.basis_vectors) | ||
and self.symbols == other.symbols | ||
and self.basis_vectors_labels == other.basis_vectors_labels | ||
) | ||
|
||
|
||
@typechecked | ||
class VariableSizeBasis(Basis, ABC): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you provide me with an example where this didn't work (for my culture)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem came from somewhere else but I had kept the change. I discard the change and all the tests passed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the change was not discarded though ?