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

Add GHZ circuit function #1499

Merged
merged 18 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
7 changes: 7 additions & 0 deletions doc/source/api-reference/qibo.rst
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ and the second will act to ``(1, 2)`` corresponding to

[Y(1), Z(2), CNOT(1, 2), H(1), H(2)]

Greenberger-Horne-Zeilinger (GHZ) state
"""""""""""""""""""""""""""""""""""""""

.. autoclass:: qibo.models.encodings.ghz_state
:members:
:member-order: bysource
renatomello marked this conversation as resolved.
Show resolved Hide resolved

.. _applicationspecific:

Quantum Fourier Transform (QFT)
Expand Down
28 changes: 28 additions & 0 deletions src/qibo/models/encodings.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,3 +470,31 @@ def _parametrized_two_qubit_gate(gate, q0, q1, params=None):
return gate(q0, q1, *params)

return gate(q0, q1)


def ghz_state(nqubits: int, **kwargs):
"""Generates an :math:`n`-qubits Greenberger-Horne-Zeilinger (GHZ) state that takes the form
Copy link
Contributor

@renatomello renatomello Oct 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"""Generates an :math:`n`-qubits Greenberger-Horne-Zeilinger (GHZ) state that takes the form
"""Generate a circuit that encodes a :math:`n`-qubit Greenberger-Horne-Zeilinger (GHZ) state.
This state that takes the form

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mho291 a tip: you can click the "Commit suggestion" button here to directly commit a suggestion to the code instead of changing things locally and then pushing it. It also helps to track which changes were already implemented.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @renatomello ! I wanted to do extra checks for the documentation, so I did it locally and generated the html file. :)


.. math::
|GHZ\\rangle_n = \\frac{|0\\rangle ^{\\otimes n} + |1\\rangle ^{\\otimes n}}{\\sqrt{2}}
renatomello marked this conversation as resolved.
Show resolved Hide resolved

where :math:`n` is the number of qubits.

Args:
nqubits (int): number of qubits, nqubits >= 2.
renatomello marked this conversation as resolved.
Show resolved Hide resolved
kwargs (dict, optional): Additional arguments used to initialize a Circuit object.
For details, see the documentation of :class:`qibo.models.circuit.Circuit`.

Returns:
:class:`qibo.models.circuit.Circuit`: GHZ state
renatomello marked this conversation as resolved.
Show resolved Hide resolved
"""
if nqubits < 2:
raise_error(
ValueError,
f"nqubits given as {nqubits}. nqubits needs to be >= 2.",
)
else:
circuit = Circuit(nqubits)
circuit.add(gates.H(0))
circuit.add(gates.CNOT(qubit, qubit + 1) for qubit in range(nqubits - 1))
return circuit
23 changes: 23 additions & 0 deletions tests/test_models_encodings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from qibo.models.encodings import (
comp_basis_encoder,
entangling_layer,
ghz_state,
phase_encoder,
unary_encoder,
unary_encoder_random_gaussian,
Expand Down Expand Up @@ -279,3 +280,25 @@ def test_circuit_kwargs(density_matrix):

test = unary_encoder_random_gaussian(4, density_matrix=density_matrix)
assert test.density_matrix is density_matrix


@pytest.mark.parametrize("density_matrix", [False, True])
@pytest.mark.parametrize("nqubits", [1, 2, 3, 4])
def test_ghz_circuit(backend, nqubits, density_matrix):
if nqubits < 2:
with pytest.raises(ValueError):
GHZ_circ = ghz_state(nqubits, density_matrix=density_matrix)
else:
target = np.zeros(2**nqubits, dtype=complex)
target[0] = 1 / np.sqrt(2)
target[2**nqubits - 1] = 1 / np.sqrt(2)
target = backend.cast(target, dtype=target.dtype)

GHZ_circ = ghz_state(nqubits, density_matrix=density_matrix)
state = backend.execute_circuit(GHZ_circ).state()

if density_matrix:
target = backend.np.outer(target, backend.np.conj(target.T))
state = backend.np.outer(state, backend.np.conj(state.T))
renatomello marked this conversation as resolved.
Show resolved Hide resolved

backend.assert_allclose(state, target)