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

New backend specialised for Hamming-weight-preserving circuits #1565

Open
wants to merge 91 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
91 commits
Select commit Hold shift + click to select a range
8a7d2f2
backend
renatomello Jan 25, 2025
066a7e5
backend
renatomello Jan 25, 2025
9f8108c
Merge branch 'hwk_backend' of github.com:qiboteam/qibo into hwk_backend
renatomello Jan 25, 2025
8e7f0bd
backup
renatomello Jan 25, 2025
9f11a67
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 25, 2025
0b5ff5d
strings
renatomello Jan 25, 2025
da7c91b
Merge branch 'hwk_backend' of github.com:qiboteam/qibo into hwk_backend
renatomello Jan 25, 2025
9fabecb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 25, 2025
9cd354c
Merge branch 'master' into hwk_backend
renatomello Jan 27, 2025
f774ce4
Merge branch 'master' into hwk_backend
renatomello Jan 29, 2025
2a3d10a
backend
renatomello Jan 25, 2025
9f2f51e
backup
renatomello Jan 25, 2025
a707aa6
strings
renatomello Jan 25, 2025
2dfaa16
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 25, 2025
2f983e2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 25, 2025
db5f901
Merge branch 'hwk_backend' of github.com:qiboteam/qibo into hwk_backend
renatomello Jan 29, 2025
f03ea0d
backup
renatomello Jan 31, 2025
0b83a3c
backup
renatomello Jan 31, 2025
f46c5b8
backup
renatomello Jan 31, 2025
d0e197e
functioning with single-qubit gates
renatomello Jan 31, 2025
72681ae
lexicographical order in the encoder
renatomello Feb 2, 2025
35c86bd
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 2, 2025
bb75648
Merge branch 'master' into hwk_backend
renatomello Feb 3, 2025
11e068b
Merge branch 'master' into hwk_backend
renatomello Feb 3, 2025
b78f3d4
Merge branch 'master' into hwk_backend
renatomello Feb 7, 2025
9d97081
Merge branch 'master' into hwk_backend
renatomello Feb 10, 2025
fa7e891
fix lint
AlejandroSopena Feb 28, 2025
f552bd9
generalize to n-qubit unitaries
AlejandroSopena Feb 28, 2025
d2123a5
fix test_backends_clifford
AlejandroSopena Feb 28, 2025
42ec088
apply_gate with n qubits
AlejandroSopena Feb 28, 2025
1b32628
fix test_quantum_info_clifford
AlejandroSopena Feb 28, 2025
41b9ac1
fix strings and indexes
AlejandroSopena Mar 3, 2025
87cbb60
compute probabilities and collapse
AlejandroSopena Mar 6, 2025
2e730ec
apply_hamming_weight method
AlejandroSopena Mar 6, 2025
7ea00d7
add HammingWeightResult class
AlejandroSopena Mar 6, 2025
56c87ef
Merge branch 'master' into hwk_backend
AlejandroSopena Mar 6, 2025
2bd90e3
compute samples and frequencies
AlejandroSopena Mar 10, 2025
860b91c
add apply_gate_CCZ
AlejandroSopena Mar 11, 2025
ab2c439
hamming_weight property
AlejandroSopena Mar 11, 2025
f05dbc1
Merge branch 'master' into hwk_backend
AlejandroSopena Mar 11, 2025
629dcf2
test hamming_weight property
AlejandroSopena Mar 13, 2025
2b8450b
get full state
AlejandroSopena Mar 13, 2025
4a1c822
compatible with cupy and numba
AlejandroSopena Mar 13, 2025
feb7ee4
Update src/qibo/gates/abstract.py
renatomello Mar 14, 2025
55be7d0
update 00 and 11 phases
renatomello Mar 14, 2025
1676498
docs
renatomello Mar 14, 2025
57ac3c7
fix _apply_gate_n_qubit
AlejandroSopena Mar 14, 2025
17178f2
testing apply_gate
AlejandroSopena Mar 14, 2025
cf00b8f
test gates
AlejandroSopena Mar 14, 2025
1c99350
fix probabilities
AlejandroSopena Mar 14, 2025
0aa327d
test measurements
AlejandroSopena Mar 17, 2025
3d19d16
test collapse
AlejandroSopena Mar 17, 2025
7347ede
test n qubit gates
AlejandroSopena Mar 17, 2025
a1ed6e9
test errors
AlejandroSopena Mar 17, 2025
917ee77
test probabilities_from_samples
AlejandroSopena Mar 17, 2025
f00e3e6
fix apply_two_qubit_gate
AlejandroSopena Mar 18, 2025
9f14451
coverage
AlejandroSopena Mar 18, 2025
600697a
coverage
AlejandroSopena Mar 18, 2025
0b5e2a1
Merge branch 'master' into hwk_backend
renatomello Mar 19, 2025
8542dee
Merge branch 'master' into hwk_backend
renatomello Mar 19, 2025
9baaa99
coverage
AlejandroSopena Mar 19, 2025
f1bc720
helper function to compute two qubit amplitudes
AlejandroSopena Mar 19, 2025
9951e43
update documentation
AlejandroSopena Mar 19, 2025
221f67a
Merge branch 'master' into hwk_backend
AlejandroSopena Mar 19, 2025
17fac8b
fix merge conflic
AlejandroSopena Mar 19, 2025
42d380c
Merge branch 'master' into hwk_backend
renatomello Mar 20, 2025
1140497
Update doc/source/api-reference/qibo.rst
renatomello Mar 20, 2025
2429d3b
Update doc/source/api-reference/qibo.rst
renatomello Mar 20, 2025
447aacf
Update doc/source/api-reference/qibo.rst
renatomello Mar 20, 2025
ae78f4d
docstrings + remove unused imports
renatomello Mar 20, 2025
30c9e83
Merge branch 'hwk_backend' of github.com:qiboteam/qibo into hwk_backend
renatomello Mar 20, 2025
7c8f514
docstrings
renatomello Mar 20, 2025
dededbc
dtype docstring
renatomello Mar 20, 2025
633bde4
cast docstring
renatomello Mar 20, 2025
7788ae0
Merge branch 'master' into hwk_backend
renatomello Mar 21, 2025
0f304d6
Merge branch 'master' into hwk_backend
renatomello Mar 21, 2025
e1a22cb
Merge branch 'master' into hwk_backend
renatomello Mar 22, 2025
366efa7
tidy up
renatomello Mar 22, 2025
4e1c8a1
tidying up
renatomello Mar 22, 2025
9abccf4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 22, 2025
393a847
lint
renatomello Mar 24, 2025
f3423be
docstrings
renatomello Mar 24, 2025
2b7d315
fix circular import
renatomello Mar 24, 2025
292b351
add hamming weight object to init
renatomello Mar 24, 2025
13cc58f
docstrings
renatomello Mar 24, 2025
cebd6b4
docstrings
renatomello Mar 24, 2025
f25ad1d
docstrings
renatomello Mar 24, 2025
3876445
refactor api ref
renatomello Mar 24, 2025
1a4146e
update docstrings
AlejandroSopena Mar 25, 2025
1f0742e
Update src/qibo/backends/hamming_weight.py
AlejandroSopena Mar 25, 2025
ebda3cf
Update src/qibo/backends/hamming_weight.py
AlejandroSopena Mar 25, 2025
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
64 changes: 64 additions & 0 deletions doc/source/api-reference/qibo.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2703,6 +2703,70 @@ Alternatively, a Clifford circuit can also be executed starting from the :class:
:members:
:member-order: bysource

.. _Hamming-weight:

Simulation of Hamming-weight-preserving circuits
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This specialised backend in ``qibo`` supports the fast simulation of
Hamming-weight-preserving circuits.
Given a quantum state that only contains non-zero amplitudes associated with computational basis
states of Hamming weight :math:`k`, the :class:`qibo.backends.hamming_weight.HammingWeightBackend`
a compressed representation of quantum states that is restricted to the subspace of
with fixed Hamming weight :math:`k`.
The dimension of this subspace is :math:`d = \binom{n}{k}`,
where :math:`n` is the total number of qubits.

As for the other backends, the Hamming-weight backend can be set
by specifying the engine used for calculation, *i.e.*

.. testcode:: python

from qibo import set_backend

set_backend("hamming_weight", platform="numpy")

If not provided, the current global backend is used:

.. testcode:: python

from qibo import set_backend
from qibo.backends import HammingWeightBackend

# setting numpy as the global backend
set_backend("numpy")
# the Hamming-weight backend will use the numpy backend as engine
backend = HammingWeightBackend()


.. autoclass:: qibo.backends.hamming_weight.HammingWeightBackend
:members:
:member-order: bysource


The results of execution of a circuit through this backend creates a
:class:`qibo.quantum_info.hamming_weight.HammingWeightResult`
object that gives access to the final state through the
:meth:`qibo.quantum_info.hamming_weight.HammingWeightResult.state` method.
The probabilities are computed by the
:meth:`qibo.quantum_info.hamming_weight.HammingWeightResult.probabilities` method.
If there are no measurements in the circuit or the number of shots is :math:`0`,
they are calculated from the statevector.
Otherwise, they are calculated from the samples.
The final measured samples and frequencies are accessible, respectively, through the
:meth:`qibo.quantum_info.hamming_weight.HammingWeightResult.samples` and the
:meth:`qibo.quantum_info.hamming_weight.HammingWeightResult.frequencies` methods.

It is also possible to recover the standard state representation with the
:meth:`qibo.quantum_info.hamming_weight.HammingWeightResult.full_state` method.
Note, however, that this process is inefficient as it involves the construction of
a statevector with size exponential in the number of qubits.

.. autoclass:: qibo.quantum_info.hamming_weight.HammingWeightResult
:members:
:member-order: bysource


Cloud Backends
^^^^^^^^^^^^^^

Expand Down
19 changes: 14 additions & 5 deletions src/qibo/backends/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from qibo.backends.abstract import Backend
from qibo.backends.clifford import CliffordBackend
from qibo.backends.hamming_weight import HammingWeightBackend
from qibo.backends.npmatrices import NumpyMatrices
from qibo.backends.numpy import NumpyBackend
from qibo.config import log, raise_error
Expand All @@ -32,18 +33,22 @@ def load(backend: str, **kwargs) -> Backend:
:class:`qibo.backends.abstract.Backend`: Loaded backend.
"""

if backend not in QIBO_NATIVE_BACKENDS + ("clifford",):
if backend not in QIBO_NATIVE_BACKENDS + ("clifford", "hamming_weight"):
raise_error(
ValueError,
f"Backend {backend} is not available. "
+ f"The native qibo backends are {QIBO_NATIVE_BACKENDS + ('clifford',)}",
+ f"The native qibo backends are {QIBO_NATIVE_BACKENDS + ('clifford', 'hamming_weight')}",
)

if backend in ["clifford", "hamming_weight"]:
backend_class = (
CliffordBackend if backend == "clifford" else HammingWeightBackend
)

if backend == "clifford":
engine = kwargs.pop("platform", None)
kwargs["engine"] = engine

return CliffordBackend(**kwargs)
return backend_class(**kwargs)

if backend == "qulacs":
from qibo.backends.qulacs import QulacsBackend # pylint: disable=C0415
Expand Down Expand Up @@ -287,6 +292,10 @@ def _check_backend(backend):
return backend


def _get_engine_name(backend):
return backend.platform if backend.platform is not None else backend.name


def list_available_backends(*providers: str) -> dict:
"""Lists all the backends that are available."""
available_backends = MetaBackend().list_available()
Expand All @@ -309,7 +318,7 @@ def construct_backend(backend, **kwargs) -> Backend: # pylint: disable=R1710
Returns:
qibo.backends.abstract.Backend: The loaded backend.
"""
if backend in QIBO_NATIVE_BACKENDS + ("clifford",):
if backend in QIBO_NATIVE_BACKENDS + ("clifford", "hamming_weight"):
return MetaBackend.load(backend, **kwargs)

provider = backend.replace("-", "_")
Expand Down
2 changes: 1 addition & 1 deletion src/qibo/backends/_clifford_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ def M(state, qubits, nqubits, collapse=False):
return sample


def cast(x, dtype=None, copy=False):
def cast(x, dtype=None, copy: bool = False):
if dtype is None:
dtype = "complex128"
if isinstance(x, np.ndarray):
Expand Down
10 changes: 6 additions & 4 deletions src/qibo/backends/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,14 @@ def set_threads(self, nthreads): # pragma: no cover
raise_error(NotImplementedError)

@abc.abstractmethod
def cast(self, x, copy=False): # pragma: no cover
"""Cast an object as the array type of the current backend.
def cast(self, x, dtype=None, copy=False): # pragma: no cover
"""Cast an object as the array or tensor type of the current backend.

Args:
x: Object to cast to array.
copy (bool): If ``True`` a copy of the object is created in memory.
x: Object to cast to array or tensor.
dtype (optional): data type of the array or tensor. If ``None``, defaults
to the default data type of the current backend. Defaults to ``None``.
copy (bool, optional): If ``True`` a copy of the object is created in memory.
"""
raise_error(NotImplementedError)

Expand Down
11 changes: 6 additions & 5 deletions src/qibo/backends/clifford.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
from qibo.config import raise_error


def _get_engine_name(backend):
return backend.platform if backend.platform is not None else backend.name


class CliffordBackend(NumpyBackend):
"""Backend for the simulation of Clifford circuits following
`Aaronson & Gottesman (2004) <https://arxiv.org/abs/quant-ph/0406196>`_.
Expand All @@ -35,7 +31,10 @@ def __init__(self, engine=None):
self._stim = stim
else:
if engine is None:
from qibo.backends import _check_backend # pylint: disable=C0415
from qibo.backends import ( # pylint: disable=C0415
_check_backend,
_get_engine_name,
)

engine = _get_engine_name(_check_backend(engine))

Expand Down Expand Up @@ -80,6 +79,8 @@ def cast(self, x, dtype=None, copy: bool = False):

Args:
x: Object to cast to array.
dtype (optional): data type of the array or tensor. If ``None``, defaults
to the default data type of the current backend. Defaults to ``None``.
copy (bool, optional): If ``True`` a copy of the object is created in memory.
Defaults to ``False``.
"""
Expand Down
Loading