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

Removed lil sparse matrix for GHZ state. #637

Merged
merged 3 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions docs/intro_tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ is a well-known 3-qubit quantum state. We can invoke this using :code:`toqito` a
.. code-block:: python

>>> from toqito.states import ghz
>>> ghz(2, 3).toarray()
>>> ghz(2, 3)
array([[0.70710678],
[0. ],
[0. ],
Expand Down Expand Up @@ -169,7 +169,7 @@ state.
.. code-block:: python

>>> from toqito.states import ghz
>>> ghz(4, 7, np.array([1, 2, 3, 4]) / np.sqrt(30)).toarray()
>>> ghz(4, 7, np.array([1, 2, 3, 4]) / np.sqrt(30))
array([[0.18257419],
[0. ],
[0. ],
Expand Down
24 changes: 11 additions & 13 deletions toqito/states/ghz.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
"""GHZ state."""

import numpy as np
from scipy import sparse


def ghz(dim: int, num_qubits: int, coeff: list[int] = None) -> sparse.lil_array:
def ghz(dim: int, num_qubits: int, coeff: list[int] | None = None) -> np.ndarray:
r"""Generate a (generalized) GHZ state :cite:`Greenberger_2007_Going`.

Returns a :code:`num_qubits`-partite GHZ state acting on :code:`dim` local dimensions, described
in :cite:`Greenberger_2007_Going`. For example, :code:`ghz(2, 3)` returns the standard 3-qubit GHZ state on qubits.
The output of this function is sparse.
The output of this function is a dense NumPy array.

For a system of :code:`num_qubits` qubits (i.e., :code:`dim = 2`), the GHZ state can be written
as
Expand All @@ -29,7 +28,7 @@ def ghz(dim: int, num_qubits: int, coeff: list[int] = None) -> sparse.lil_array:
Using :code:`toqito`, we can see that this yields the proper state.

>>> from toqito.states import ghz
>>> ghz(2, 3).toarray()
>>> ghz(2, 3)
array([[0.70710678],
[0. ],
[0. ],
Expand All @@ -50,7 +49,7 @@ def ghz(dim: int, num_qubits: int, coeff: list[int] = None) -> sparse.lil_array:

>>> from toqito.states import ghz
>>> import numpy as np
>>> ghz(4, 7, np.array([1, 2, 3, 4]) / np.sqrt(30)).toarray()
>>> ghz(4, 7, np.array([1, 2, 3, 4]) / np.sqrt(30))
array([[0.18257419],
[0. ],
[0. ],
Expand Down Expand Up @@ -85,13 +84,12 @@ def ghz(dim: int, num_qubits: int, coeff: list[int] = None) -> sparse.lil_array:
if len(coeff) != dim:
raise ValueError("InvalidCoeff: The variable `coeff` must be a vector of length equal to `dim`.")

# Construct the state (and do it in a way that is less memory-intensive
# than naively tensoring things together.
dim_sum = 1
for i in range(1, num_qubits):
dim_sum += dim**i
# Initialize the GHZ state vector.
ret_ghz_state = np.zeros((dim**num_qubits, 1))

# Fill the GHZ state vector with the appropriate coefficients.
for i in range(dim):
index = sum(i * dim**k for k in range(num_qubits))
ret_ghz_state[index] = coeff[i]

ret_ghz_state = sparse.lil_array((dim**num_qubits, 1))
for i in range(1, dim + 1):
ret_ghz_state[(i - 1) * dim_sum] = coeff[i - 1]
return ret_ghz_state
4 changes: 2 additions & 2 deletions toqito/states/tests/test_ghz.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
)
def test_ghz(dim, num_qubits, coeff, expected_res):
"""Test function works as expected for a valid input."""
res = ghz(dim, num_qubits, coeff).toarray()
res = ghz(dim, num_qubits, coeff)
np.testing.assert_allclose(res, expected_res)


Expand All @@ -44,7 +44,7 @@ def test_ghz_4_7():
)
)

res = ghz(4, 7, np.array([1, 2, 3, 4]) / np.sqrt(30)).toarray()
res = ghz(4, 7, np.array([1, 2, 3, 4]) / np.sqrt(30))
np.testing.assert_allclose(res, expected_res)


Expand Down