Skip to content

Commit 473bcf7

Browse files
authored
Merge pull request #1499 from qiboteam/ghz
Add `GHZ` circuit function
2 parents 4cbe715 + ca60373 commit 473bcf7

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

doc/source/api-reference/qibo.rst

+5
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,11 @@ with the last qubit as the control qubit and the first qubit as a target qubit.
401401
.. autofunction:: qibo.models.encodings.entangling_layer
402402

403403

404+
Greenberger-Horne-Zeilinger (GHZ) state
405+
"""""""""""""""""""""""""""""""""""""""
406+
407+
.. autofunction:: qibo.models.encodings.ghz_state
408+
404409
.. _error-mitigation:
405410

406411
Error Mitigation

src/qibo/models/encodings.py

+29
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,35 @@ def entangling_layer(
376376
return circuit
377377

378378

379+
def ghz_state(nqubits: int, **kwargs):
380+
"""Generates an :math:`n`-qubit Greenberger-Horne-Zeilinger (GHZ) state that takes the form
381+
382+
.. math::
383+
\\ket{\\text{GHZ}} = \\frac{\\ket{0}^{\\otimes n} + \\ket{1}^{\\otimes n}}{\\sqrt{2}}
384+
385+
where :math:`n` is the number of qubits.
386+
387+
Args:
388+
nqubits (int): number of qubits :math:`n >= 2`.
389+
kwargs (dict, optional): additional arguments used to initialize a Circuit object.
390+
For details, see the documentation of :class:`qibo.models.circuit.Circuit`.
391+
392+
Returns:
393+
:class:`qibo.models.circuit.Circuit`: Circuit that prepares the GHZ state.
394+
"""
395+
if nqubits < 2:
396+
raise_error(
397+
ValueError,
398+
f"nqubits given as {nqubits}. nqubits needs to be >= 2.",
399+
)
400+
401+
circuit = Circuit(nqubits, **kwargs)
402+
circuit.add(gates.H(0))
403+
circuit.add(gates.CNOT(qubit, qubit + 1) for qubit in range(nqubits - 1))
404+
405+
return circuit
406+
407+
379408
def _generate_rbs_pairs(nqubits: int, architecture: str, **kwargs):
380409
"""Generating list of indexes representing the RBS connections
381410

tests/test_models_encodings.py

+22
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from qibo.models.encodings import (
1212
comp_basis_encoder,
1313
entangling_layer,
14+
ghz_state,
1415
phase_encoder,
1516
unary_encoder,
1617
unary_encoder_random_gaussian,
@@ -279,3 +280,24 @@ def test_circuit_kwargs(density_matrix):
279280

280281
test = unary_encoder_random_gaussian(4, density_matrix=density_matrix)
281282
assert test.density_matrix is density_matrix
283+
284+
285+
@pytest.mark.parametrize("density_matrix", [False, True])
286+
@pytest.mark.parametrize("nqubits", [1, 2, 3, 4])
287+
def test_ghz_circuit(backend, nqubits, density_matrix):
288+
if nqubits < 2:
289+
with pytest.raises(ValueError):
290+
GHZ_circ = ghz_state(nqubits, density_matrix=density_matrix)
291+
else:
292+
target = np.zeros(2**nqubits, dtype=complex)
293+
target[0] = 1 / np.sqrt(2)
294+
target[2**nqubits - 1] = 1 / np.sqrt(2)
295+
target = backend.cast(target, dtype=target.dtype)
296+
297+
GHZ_circ = ghz_state(nqubits, density_matrix=density_matrix)
298+
state = backend.execute_circuit(GHZ_circ).state()
299+
300+
if density_matrix:
301+
target = backend.np.outer(target, backend.np.conj(target.T))
302+
303+
backend.assert_allclose(state, target)

0 commit comments

Comments
 (0)