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

Feat custom basis #101

Merged
merged 105 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from 49 commits
Commits
Show all changes
105 commits
Select commit Hold shift + click to select a range
780b39e
basis measure implementation start
Henri-ColibrITD Feb 27, 2024
e68b109
solved the problem of nullable qubits numbers
Henri-ColibrITD Feb 28, 2024
21c1f61
details
Henri-ColibrITD Feb 28, 2024
bf2c777
removed circular dependencies
Henri-ColibrITD Feb 28, 2024
1fa0318
feat: mpqp to qasm
JulienCalistoTD Sep 12, 2024
b6b5136
doc: mpqp_to_qasm2
JulienCalistoTD Sep 18, 2024
bf9585c
chore: Files formated
github-actions[bot] Sep 18, 2024
f5db2e6
Merge branch 'dev' into feat-mpqp-to-qasm
JulienCalistoTD Sep 26, 2024
304e9d4
test: random circuit for mpqp_to_qasm
JulienCalistoTD Sep 26, 2024
d79082f
chore: Files formated
github-actions[bot] Sep 26, 2024
dae6b43
fix: add CustomGate and replace to_qasm2
JulienCalistoTD Sep 26, 2024
0fcfa52
chore: add Language QASM2 and QASM3
JulienCalistoTD Sep 30, 2024
d6da0d6
doc: refinements
Henri-ColibrITD Oct 2, 2024
0cb2b68
Merge branch 'feat-mpqp-to-qasm' of https://github.com/ColibrITD-SAS/…
Henri-ColibrITD Oct 2, 2024
2dc4bb3
chore: add QASM2 to_other_language for instruction
JulienCalistoTD Oct 2, 2024
737b727
test: fix test_mpqp_to_qasm
JulienCalistoTD Oct 2, 2024
9da0f9e
chore: typechecks
JulienCalistoTD Oct 2, 2024
3e9eafc
chore: remove qcircuit to_qasm2 and 3
JulienCalistoTD Oct 2, 2024
576a8df
chore: Files formated
github-actions[bot] Oct 2, 2024
ba88279
Merge branch 'dev' into feat-mpqp-to-qasm
JulienCalistoTD Oct 21, 2024
7b6a4f8
Merge branch 'feat-mpqp-to-qasm' of https://github.com/ColibrITD-SAS/…
JulienCalistoTD Oct 21, 2024
67032f5
Merge remote-tracking branch 'origin/dev' into feat-custom-basis
hJaffaliColibritd Oct 21, 2024
168d420
chore: formatting
Henri-ColibrITD Oct 28, 2024
0666403
Merge branch 'feat-mpqp-to-qasm' of https://github.com/ColibrITD-SAS/…
Henri-ColibrITD Oct 28, 2024
8693c41
chore: `if` denesting
Henri-ColibrITD Oct 29, 2024
a60fcaa
chore: update to_other_language, review basis condition
MoHermes Oct 29, 2024
d820162
fix: BasisMeasure class support custom basis
MoHermes Oct 29, 2024
8de5261
fix: handle custom basis in generate_job
MoHermes Oct 29, 2024
fdd86ae
fix: pass Breakpoint and ExpectationMeasure to QASM2
JulienCalistoTD Oct 29, 2024
17f3a87
fix: handle \n in circuit
JulienCalistoTD Oct 29, 2024
f0acef7
chore: details
Henri-ColibrITD Oct 30, 2024
b5abe8c
Merge branch 'dev' into feat-custom-basis
Henri-ColibrITD Oct 30, 2024
ee29c65
feat: basis are now dynamic
Henri-ColibrITD Oct 30, 2024
24e64f7
chore: specify custom symbols in basis state representations/printed …
MoHermes Oct 30, 2024
bb92906
Merge branch 'feat-custom-basis' of github.com:ColibrITD-SAS/mpqp int…
MoHermes Oct 30, 2024
6cf84cb
fix: problem with the Hadamard basis, WIP
Henri-ColibrITD Oct 30, 2024
9c1e273
Merge branch 'feat-custom-basis' of https://github.com/ColibrITD-SAS/…
Henri-ColibrITD Oct 30, 2024
a46298f
fix: dynamic hadamard basis now works
Henri-ColibrITD Oct 30, 2024
5941511
fix: handle simplify BasisMeasure, add test, and chore
JulienCalistoTD Oct 29, 2024
690baee
merge
JulienCalistoTD Oct 30, 2024
982cf13
chore: type-checker
JulienCalistoTD Oct 30, 2024
c2ae312
fix: test failing because of new condition added in result.__str__
Henri-ColibrITD Oct 30, 2024
1703a72
test: to_other_language is implemented for qiskit
MoHermes Oct 30, 2024
78f9cae
chore: Files formated
github-actions[bot] Oct 30, 2024
550528d
fix: correct assignment of 'symbols'
MoHermes Oct 30, 2024
eca0925
Merge branch 'feat-custom-basis' of github.com:ColibrITD-SAS/mpqp int…
MoHermes Oct 30, 2024
ef89022
doc: update documentation for 'Basis' class methods
MoHermes Oct 31, 2024
1cf90d0
doc: update documentation for 'VariableSizeBasis' class
MoHermes Oct 31, 2024
4bc491d
chore: clean up tests, removing unneeded cases
MoHermes Oct 31, 2024
9a25608
test: add a test case for custom basis execution
MoHermes Oct 31, 2024
c5016e9
chore: enhance overall test readability
MoHermes Nov 4, 2024
3069965
chore: change self.parameter to k
JulienCalistoTD Nov 4, 2024
9df59e0
Merge branch 'dev' into feat-mpqp-to-qasm
JulienCalistoTD Nov 4, 2024
3910756
chore: Files formated
github-actions[bot] Nov 4, 2024
3b62931
Merge branch 'dev' into feat-mpqp-to-qasm
hJaffaliColibritd Nov 4, 2024
49e0c44
chore: deleted temp file
Henri-ColibrITD Nov 4, 2024
ee4d43a
Merge branch 'feat-custom-basis' of https://github.com/ColibrITD-SAS/…
Henri-ColibrITD Nov 4, 2024
47f1f1e
test: custom base test made more generic to help making other
Henri-ColibrITD Nov 4, 2024
e44eb22
Merge branch 'dev' into feat-custom-basis
Henri-ColibrITD Nov 4, 2024
995f5c0
fix: merge issue
Henri-ColibrITD Nov 4, 2024
268226e
doc: example detail
Henri-ColibrITD Nov 4, 2024
dad8458
fix: repr
Henri-ColibrITD Nov 4, 2024
ccabd60
doc: add examples demonstrating basis with custom symbols
MoHermes Nov 5, 2024
701126a
test: run with different circuit and basis vectors
MoHermes Nov 5, 2024
aba71e2
chore: moving attributes and properties at the top of U class
hJaffaliColibritd Nov 5, 2024
d02b997
chore: format
JulienCalistoTD Nov 5, 2024
ab71090
fix: remove OpenQASMTranslationWarning for non user
JulienCalistoTD Nov 5, 2024
161e757
test: add custom gate test
JulienCalistoTD Nov 6, 2024
cee7d2b
fix: basis measure at the end of circuit + chore
JulienCalistoTD Nov 7, 2024
88c7b23
chore: remove qcircuit arg
JulienCalistoTD Nov 7, 2024
5a4d0c2
fix: hardcode test, + repr barrier and rotation gate
JulienCalistoTD Nov 7, 2024
60dcdc8
Merge remote-tracking branch 'origin/dev' into feat-custom-basis
hJaffaliColibritd Nov 8, 2024
b670855
add: test for CustomGate and U
JulienCalistoTD Nov 12, 2024
d470414
chore: Files formated
github-actions[bot] Nov 12, 2024
c26400f
test: basis tests on SV instead of samples
Henri-ColibrITD Nov 12, 2024
488f083
feat: custom basis doesn't need the target anymore
Henri-ColibrITD Nov 14, 2024
fed330e
fix: handle all basis
MoHermes Nov 14, 2024
05580d8
Merge branch 'feat-custom-basis' of github.com:ColibrITD-SAS/mpqp int…
MoHermes Nov 14, 2024
5eed848
fix: BasisMeasure with 0 shots on cirq now works properly
Henri-ColibrITD Nov 14, 2024
b622b91
Merge branch 'feat-custom-basis' of https://github.com/ColibrITD-SAS/…
Henri-ColibrITD Nov 14, 2024
da6d0a0
fix: handle all basis, successful run on google-cirq
MoHermes Nov 14, 2024
2433cb9
Merge branch 'feat-custom-basis' of github.com:ColibrITD-SAS/mpqp int…
MoHermes Nov 14, 2024
0601282
doc: update doc of _update_targets_components
hJaffaliColibritd Nov 15, 2024
0a9f749
chore: Files formated
github-actions[bot] Nov 15, 2024
11d5a2a
test: adding some tests for statevector and sampling jobs with custom…
hJaffaliColibritd Nov 15, 2024
abd8fd3
chore: adding a fixme
hJaffaliColibritd Nov 15, 2024
a4860ef
chore: Files formated
github-actions[bot] Nov 15, 2024
bb17fef
fix: orthogonal basis detection for complex vectors
Henri-ColibrITD Nov 15, 2024
359c0c4
doc: forgotten word
Henri-ColibrITD Nov 15, 2024
b30b039
chore: 'Basis' class support basis_vectors_labels
MoHermes Nov 15, 2024
65d8c2f
Merge branch 'feat-custom-basis' of github.com:ColibrITD-SAS/mpqp int…
MoHermes Nov 15, 2024
f14861a
fix: trivial error
Henri-ColibrITD Nov 15, 2024
fbacf8c
Merge branch 'feat-custom-basis' of https://github.com/ColibrITD-SAS/…
Henri-ColibrITD Nov 15, 2024
2ac0bc3
fix: tests should now pass
Henri-ColibrITD Nov 15, 2024
f349667
Merge branch 'dev' into feat-custom-basis
Henri-ColibrITD Nov 15, 2024
45ec19c
test: test made more generic
Henri-ColibrITD Nov 15, 2024
5ceb7ba
chore: algebraic mistakes and other details
Henri-ColibrITD Nov 18, 2024
2929050
Merge branch 'feat-custom-basis' into feat-mpqp-to-qasm
JulienCalistoTD Nov 18, 2024
36a3add
chore: Files formated
github-actions[bot] Nov 18, 2024
dae3caa
chore: reverted wrong modifications
Henri-ColibrITD Nov 18, 2024
2acd489
doc: job.circuit clarification
Henri-ColibrITD Nov 18, 2024
ab443cd
fix: basisMeasure for qasm
JulienCalistoTD Nov 19, 2024
8292ad5
Merge branch 'feat-mpqp-to-qasm' of https://github.com/ColibrITD-SAS/…
JulienCalistoTD Nov 19, 2024
9151c66
chore: typecheck + clean
JulienCalistoTD Nov 19, 2024
9ef338e
Merge remote-tracking branch 'origin/feat-mpqp-to-qasm' into feat-cus…
Henri-ColibrITD Nov 19, 2024
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
82 changes: 44 additions & 38 deletions mpqp/core/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,6 @@
from typing import TYPE_CHECKING, Iterable, Optional, Sequence, Type
from warnings import warn

if TYPE_CHECKING:
from qat.core.wrappers.circuit import Circuit as myQLM_Circuit
from cirq.circuits.circuit import Circuit as cirq_Circuit
from braket.circuits import Circuit as braket_Circuit
from qiskit.circuit import QuantumCircuit
from sympy import Basic, Expr

import numpy as np
import numpy.typing as npt
from typeguard import TypeCheckError, typechecked
Expand All @@ -44,18 +37,25 @@
from mpqp.core.instruction.gates.custom_gate import CustomGate
from mpqp.core.instruction.gates.gate_definition import UnitaryMatrix
from mpqp.core.instruction.gates.parametrized_gate import ParametrizedGate
from mpqp.core.instruction.measurement import BasisMeasure, ComputationalBasis, Measure
from mpqp.core.instruction.measurement import BasisMeasure, Measure
from mpqp.core.instruction.measurement.expectation_value import ExpectationMeasure
from mpqp.core.languages import Language
from mpqp.noise.noise_model import DimensionalNoiseModel, NoiseModel
from mpqp.qasm import qasm2_to_myqlm_Circuit
from mpqp.qasm.open_qasm_2_and_3 import open_qasm_2_to_3
from mpqp.qasm.qasm_to_braket import qasm3_to_braket_Circuit
from mpqp.qasm.qasm_to_cirq import qasm2_to_cirq_Circuit
from mpqp.tools.errors import NumberQubitsError, NonReversibleWarning
from mpqp.tools.errors import NonReversibleWarning, NumberQubitsError
from mpqp.tools.generics import OneOrMany
from mpqp.tools.maths import matrix_eq

if TYPE_CHECKING:
from qat.core.wrappers.circuit import Circuit as myQLM_Circuit
from cirq.circuits.circuit import Circuit as cirq_Circuit
from braket.circuits import Circuit as braket_Circuit
from qiskit.circuit import QuantumCircuit
from sympy import Basic, Expr


@typechecked
class QCircuit:
Expand Down Expand Up @@ -155,7 +155,7 @@ def __init__(
self._nb_qubits = max(connections) + 1
else:
self._nb_qubits = nb_qubits
self.add(list(map(deepcopy, data)))
self.add(deepcopy(data))

def __eq__(self, value: object) -> bool:
return dumps(self) == dumps(value)
Expand Down Expand Up @@ -256,12 +256,15 @@ def _check_components_targets(self, components: Instruction | NoiseModel):
"In noisy circuits, BasisMeasure must span all qubits in the circuit."
)

def _update_targets_components(self, components: Instruction | NoiseModel):
"""update the targets of the components with the number of qubits in the circuit.
def _update_targets_components(self, component: Instruction | NoiseModel):
"""Update the targets of the component with the number of qubits in the circuit.

Args:
component: Instruction or NoiseModel for which we want to update the `targets` attribute.

Raises:
ValueError: If the number of target qubits for a noise source is
smaller than the its dimension, or if BasisMeasure does not span
ValueError: If the number of target qubits for a NoiseModel is
smaller than its dimension, or if BasisMeasure does not span
all qubits in a noisy circuit.

Examples:
Expand All @@ -285,28 +288,36 @@ def _update_targets_components(self, components: Instruction | NoiseModel):
"""
targets = list(range(self.nb_qubits))

components.targets = targets
self._check_components_targets(components)
component.targets = targets
self._check_components_targets(component)

if isinstance(component, Barrier):
component.size = self.nb_qubits
elif isinstance(component, ExpectationMeasure):
component.check_targets_order()
elif isinstance(component, DimensionalNoiseModel):
component.check_dimension()
elif isinstance(component, BasisMeasure):
from mpqp.core.instruction.measurement.basis import VariableSizeBasis

if not isinstance(component.basis, VariableSizeBasis):
raise ValueError(
"A `BasisMeasure` with a non variable sized basis cannot be"
" dynamic."
)

component.basis.set_size(self.nb_qubits)

if isinstance(components, Barrier):
components.size = self.nb_qubits
elif isinstance(components, ExpectationMeasure):
components.check_targets_order()
elif isinstance(components, DimensionalNoiseModel):
components.check_dimension()
elif isinstance(components, BasisMeasure):
if self.nb_cbits is None:
self.nb_cbits = 0
unique_cbits = set()
for basis_measure in self.instructions:
if basis_measure != components and isinstance(
basis_measure, BasisMeasure
):
if basis_measure.c_targets:
unique_cbits.update(basis_measure.c_targets)
for instruction in self.instructions:
if instruction != component and isinstance(instruction, BasisMeasure):
if instruction.c_targets:
unique_cbits.update(instruction.c_targets)
c_targets = []
i = 0
for _ in range(len(components.targets)):
for _ in range(len(component.targets)):
while i in unique_cbits:
warn(
"Dynamic measurements don't play well with static measurements: "
Expand All @@ -315,12 +326,12 @@ def _update_targets_components(self, components: Instruction | NoiseModel):
i += 1
c_targets.append(i)
i += 1
components.c_targets = c_targets
component.c_targets = c_targets
self.nb_cbits = max(
max(c_targets, default=0) + 1, max(unique_cbits, default=0) + 1
)

return components
return component

@property
def nb_qubits(self) -> int:
Expand Down Expand Up @@ -955,12 +966,7 @@ def to_other_language(
qargs = instruction.controls + instruction.targets
elif isinstance(instruction, Gate):
qargs = instruction.targets
elif isinstance(instruction, BasisMeasure) and isinstance(
instruction.basis, ComputationalBasis
):
# TODO for custom basis, check if something should be
# changed here, e.g. remove the condition to have only
# computational basis
elif isinstance(instruction, BasisMeasure):
assert instruction.c_targets is not None
qargs = [instruction.targets]
cargs = [instruction.c_targets]
Expand Down
149 changes: 123 additions & 26 deletions mpqp/core/instruction/measurement/basis.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@

from __future__ import annotations

from abc import abstractmethod
from abc import ABC, abstractmethod
from functools import reduce
from typing import Optional

import numpy as np
import numpy.typing as npt
from typeguard import typechecked

from mpqp.tools.display import clean_1D_array
from mpqp.core.instruction.gates.custom_gate import CustomGate
from mpqp.core.instruction.gates.gate_definition import UnitaryMatrix
from mpqp.tools.display import clean_1D_array, one_lined_repr
from mpqp.tools.maths import atol, matrix_eq


Expand All @@ -33,27 +35,41 @@ class Basis:
``basis_vectors``'s dimensions.

Example:
>>> Basis([np.array([1,0]), np.array([0,-1])]).pretty_print()
>>> custom_basis = Basis([np.array([1,0]), np.array([0,-1])], symbols=("↑", "↓"))
>>> custom_basis.pretty_print()
Basis: [
[1, 0],
[0, -1]
]
>>> circ = QCircuit([X(0), H(1), CNOT(1, 2), Y(2)])
>>> circ.add(BasisMeasure([0, 1, 2], basis=custom_basis, shots=10000))
>>> print(run(circ, IBMDevice.AER_SIMULATOR)) # doctest: +SKIP
Result: None, IBMDevice, AER_SIMULATOR
Counts: [0, 0, 0, 0, 0, 4936, 5064, 0]
Probabilities: [0, 0, 0, 0, 0, 0.4936, 0.5064, 0]
Samples:
State: ↓↑↓, Index: 5, Count: 4936, Probability: 0.4936
State: ↓↓↑, Index: 6, Count: 5064, Probability: 0.5064
Error: None

"""

def __init__(
self,
basis_vectors: list[npt.NDArray[np.complex64]],
nb_qubits: Optional[int] = None,
symbols: Optional[tuple[str, str]] = None,
):
# TODO : add the possibility to give the symbols for the '0' and '1' of
# the custom basis. This should then appear in the Sample
# binary_representation of the basis state. For instance in the Hadamard
# basis, the symbols will be '+' and '-'. If the user wants '↑' and '↓'
# for his custom basis, when we print samples we would have something
# like:
# State: ↑↑↓, Index: 1, Count: 512, Probability: 0.512
if symbols is None:
symbols = ("0", "1")
self.symbols = symbols

if len(basis_vectors) == 0:
if nb_qubits is None:
raise ValueError(
"Empty basis and no number of qubits specified. Please at "
"least specify one of these two."
)
self.nb_qubits = nb_qubits
self.basis_vectors = basis_vectors
return
Expand All @@ -71,7 +87,7 @@ def __init__(
raise ValueError("All vectors of the given basis are not normalized")
m = np.array([vector for vector in basis_vectors])
if not matrix_eq(
m.transpose().dot(m),
m.transpose().dot(m), # fixme
np.eye(len(basis_vectors)), # pyright: ignore[reportArgumentType]
):
raise ValueError("The given basis is not orthogonal")
Expand All @@ -81,6 +97,24 @@ def __init__(
self.basis_vectors = basis_vectors
"""See parameter description."""

def binary_to_custom(self, state: str) -> str:
"""Converts a binary string to a custom string representation.
By default, it uses "0" and "1" but can be customized based on the provided `symbols`.

Args:
state: The binary string (e.g., "01") to be converted.

Returns:
The custom string representation of the binary state.

Example:
>>> basis = Basis([np.array([1,0]), np.array([0,-1])], symbols=("+", "-"))
>>> custom_state = basis.binary_to_custom("01")
>>> custom_state
'+-'
"""
return ''.join(self.symbols[int(bit)] for bit in state)

def pretty_print(self):
"""Nicer print for the basis, with human readable formatting.

Expand All @@ -96,17 +130,62 @@ def pretty_print(self):
print(f"Basis: [\n {joint_vectors}\n]")

def __repr__(self) -> str:
return f"{type(self).__name__}({self.basis_vectors}, {self.nb_qubits})"
joint_vectors = ", ".join(map(one_lined_repr, self.basis_vectors))
qubits = "" if isinstance(self, VariableSizeBasis) else f", {self.nb_qubits}"
return f"{type(self).__name__}({joint_vectors}{qubits})"

def to_computational(self):
"""Converts the custom basis to the computational basis.

This method creates a quantum circuit with a custom gate represented by
a unitary transformation and applies it to all qubits before measurement.

Returns:
A quantum circuit representing the basis change circuit.

Example:
>>> basis = Basis([np.array([1, 0]), np.array([0, -1])])
>>> circuit = basis.to_computational()
>>> print(circuit)
┌─────────┐
q: ┤ Unitary ├
└─────────┘

"""

from mpqp.core.circuit import QCircuit

basis_change = np.array(self.basis_vectors).T.conjugate()
return QCircuit(
[
CustomGate(
UnitaryMatrix(basis_change), targets=list(range(self.nb_qubits))
)
]
)


@typechecked
class VariableSizeBasis(Basis):
"""3M-TODO"""
class VariableSizeBasis(Basis, ABC):
"""A variable-size basis with a dynamically adjustable size to different qubit numbers
during circuit execution.

Args:
nb_qubits: number of qubits in the basis. If not provided,
the basis can be dynamically sized later using the `set_size` method.

symbols: custom symbols for representing basis states, defaults to ("0", "1").
"""

@abstractmethod
def __init__(self, nb_qubits: Optional[int] = None):
super().__init__([], nb_qubits)
pass
def __init__(
self, nb_qubits: Optional[int] = None, symbols: Optional[tuple[str, str]] = None
):
if symbols is None:
symbols = ("0", "1")
super().__init__([], 0, symbols=symbols)
if nb_qubits is not None:
self.set_size(nb_qubits)

@abstractmethod
def set_size(self, nb_qubits: int):
Expand Down Expand Up @@ -159,10 +238,7 @@ class ComputationalBasis(VariableSizeBasis):
"""

def __init__(self, nb_qubits: Optional[int] = None):
basis = []
Basis.__init__(self, basis, nb_qubits)
if nb_qubits is not None:
self.set_size(nb_qubits)
super().__init__(nb_qubits)

def set_size(self, nb_qubits: int):
self.basis_vectors = [
Expand All @@ -171,6 +247,11 @@ def set_size(self, nb_qubits: int):
]
self.nb_qubits = nb_qubits

def to_computational(self):
from mpqp.core.circuit import QCircuit

return QCircuit(self.nb_qubits)


class HadamardBasis(VariableSizeBasis):
"""Basis representing the Hadamard basis, also called X-basis or +/- basis.
Expand All @@ -186,17 +267,33 @@ class HadamardBasis(VariableSizeBasis):
[0.5, 0.5, -0.5, -0.5],
[0.5, -0.5, -0.5, 0.5]
]

>>> circ = QCircuit([X(0), H(1), CNOT(1, 2), Y(2)])
>>> circ.add(BasisMeasure(basis=HadamardBasis()))
>>> print(run(circ, IBMDevice.AER_SIMULATOR)) # doctest: +SKIP
Result: None, IBMDevice, AER_SIMULATOR
Counts: [0, 261, 253, 0, 0, 244, 266, 0]
Probabilities: [0, 0.25488, 0.24707, 0, 0, 0.23828, 0.25977, 0]
Samples:
State: ++-, Index: 1, Count: 261, Probability: 0.2548828
State: +-+, Index: 2, Count: 253, Probability: 0.2470703
State: -+-, Index: 5, Count: 244, Probability: 0.2382812
State: --+, Index: 6, Count: 266, Probability: 0.2597656
Error: None
"""

def __init__(self, nb_qubits: Optional[int] = None):
basis = []
Basis.__init__(self, basis, nb_qubits)
if nb_qubits is not None:
self.set_size(nb_qubits)
super().__init__(nb_qubits, symbols=('+', '-'))

def set_size(self, nb_qubits: int):
H = np.array([[1, 1], [1, -1]], dtype=np.complex64) / np.sqrt(2)
Hn = reduce(np.kron, [H] * nb_qubits, np.eye(1))
self.basis_vectors = [line for line in Hn]
self.nb_qubits = nb_qubits

def to_computational(self):
from mpqp.core.circuit import QCircuit
from mpqp.core.instruction.gates.native_gates import H

if self.nb_qubits == 0:
return QCircuit(self.nb_qubits)
return QCircuit([H(qb) for qb in range(self.nb_qubits)])
Loading