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

real_amplitudes.py #11

Merged
merged 1 commit into from
Jun 12, 2023
Merged
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
89 changes: 6 additions & 83 deletions torchquantum/n_local/real_amplitudes.py
Original file line number Diff line number Diff line change
@@ -1,103 +1,29 @@

from typing import Union, Optional, List, Tuple, Callable, Any
import numpy as np
#Just a few import changes/adjustments required

from qiskit.circuit.library.standard_gates import RYGate, CXGate
from torchquantum.operators import RY, CNOT
from .two_local import TwoLocal

class RealAmplitudes(TwoLocal):
r"""The real-amplitudes 2-local circuit.

The ``RealAmplitudes`` circuit is a heuristic trial wave function used as Ansatz in chemistry
applications or classification circuits in machine learning. The circuit consists of
of alternating layers of :math:`Y` rotations and :math:`CX` entanglements. The entanglement
of alternating layers of :math:`Y` rotations and :math:`CNOT` entanglements. The entanglement
pattern can be user-defined or selected from a predefined set.
It is called ``RealAmplitudes`` since the prepared quantum states will only have
real amplitudes, the complex part is always 0.

For example a ``RealAmplitudes`` circuit with 2 repetitions on 3 qubits with ``'reverse_linear'``
entanglement is

.. parsed-literal::
┌──────────┐ ░ ░ ┌──────────┐ ░ ░ ┌──────────┐
┤ Ry(θ[0]) ├─░────────■───░─┤ Ry(θ[3]) ├─░────────■───░─┤ Ry(θ[6]) ├
├──────────┤ ░ ┌─┴─┐ ░ ├──────────┤ ░ ┌─┴─┐ ░ ├──────────┤
┤ Ry(θ[1]) ├─░───■──┤ X ├─░─┤ Ry(θ[4]) ├─░───■──┤ X ├─░─┤ Ry(θ[7]) ├
├──────────┤ ░ ┌─┴─┐└───┘ ░ ├──────────┤ ░ ┌─┴─┐└───┘ ░ ├──────────┤
┤ Ry(θ[2]) ├─░─┤ X ├──────░─┤ Ry(θ[5]) ├─░─┤ X ├──────░─┤ Ry(θ[8]) ├
└──────────┘ ░ └───┘ ░ └──────────┘ ░ └───┘ ░ └──────────┘

The entanglement can be set using the ``entanglement`` keyword as string or a list of
index-pairs. See the documentation of :class:`~qiskit.circuit.library.TwoLocal` and
:class:`~qiskit.circuit.NLocal` for more detail. Additional options that can be set include the
index-pairs.Additional options that can be set include the
number of repetitions, skipping rotation gates on qubits that are not entangled, leaving out
the final rotation layer and inserting barriers in between the rotation and entanglement
layers.

If some qubits are not entangled with other qubits it makes sense to not apply rotation gates
on these qubits, since a sequence of :math:`Y` rotations can be reduced to a single :math:`Y`
rotation with summed rotation angles.

Examples:

>>> ansatz = RealAmplitudes(3, reps=2) # create the circuit on 3 qubits
>>> print(ansatz)
┌──────────┐ ┌──────────┐ ┌──────────┐
q_0: ┤ Ry(θ[0]) ├──────────■──────┤ Ry(θ[3]) ├──────────■──────┤ Ry(θ[6]) ├
├──────────┤ ┌─┴─┐ ├──────────┤ ┌─┴─┐ ├──────────┤
q_1: ┤ Ry(θ[1]) ├──■─────┤ X ├────┤ Ry(θ[4]) ├──■─────┤ X ├────┤ Ry(θ[7]) ├
├──────────┤┌─┴─┐┌──┴───┴───┐└──────────┘┌─┴─┐┌──┴───┴───┐└──────────┘
q_2: ┤ Ry(θ[2]) ├┤ X ├┤ Ry(θ[5]) ├────────────┤ X ├┤ Ry(θ[8]) ├────────────
└──────────┘└───┘└──────────┘ └───┘└──────────┘

>>> ansatz = RealAmplitudes(3, entanglement='full', reps=2) # it is the same unitary as above
>>> print(ansatz)
┌──────────┐ ┌──────────┐ ┌──────────┐
q_0: ┤ RY(θ[0]) ├──■────■──┤ RY(θ[3]) ├──────────────■────■──┤ RY(θ[6]) ├────────────
├──────────┤┌─┴─┐ │ └──────────┘┌──────────┐┌─┴─┐ │ └──────────┘┌──────────┐
q_1: ┤ RY(θ[1]) ├┤ X ├──┼───────■──────┤ RY(θ[4]) ├┤ X ├──┼───────■──────┤ RY(θ[7]) ├
├──────────┤└───┘┌─┴─┐ ┌─┴─┐ ├──────────┤└───┘┌─┴─┐ ┌─┴─┐ ├──────────┤
q_2: ┤ RY(θ[2]) ├─────┤ X ├───┤ X ├────┤ RY(θ[5]) ├─────┤ X ├───┤ X ├────┤ RY(θ[8]) ├
└──────────┘ └───┘ └───┘ └──────────┘ └───┘ └───┘ └──────────┘

>>> ansatz = RealAmplitudes(3, entanglement='linear', reps=2, insert_barriers=True)
>>> qc = QuantumCircuit(3) # create a circuit and append the RY variational form
>>> qc.compose(ansatz, inplace=True)
>>> qc.draw()
┌──────────┐ ░ ░ ┌──────────┐ ░ ░ ┌──────────┐
q_0: ┤ RY(θ[0]) ├─░───■────────░─┤ RY(θ[3]) ├─░───■────────░─┤ RY(θ[6]) ├
├──────────┤ ░ ┌─┴─┐ ░ ├──────────┤ ░ ┌─┴─┐ ░ ├──────────┤
q_1: ┤ RY(θ[1]) ├─░─┤ X ├──■───░─┤ RY(θ[4]) ├─░─┤ X ├──■───░─┤ RY(θ[7]) ├
├──────────┤ ░ └───┘┌─┴─┐ ░ ├──────────┤ ░ └───┘┌─┴─┐ ░ ├──────────┤
q_2: ┤ RY(θ[2]) ├─░──────┤ X ├─░─┤ RY(θ[5]) ├─░──────┤ X ├─░─┤ RY(θ[8]) ├
└──────────┘ ░ └───┘ ░ └──────────┘ ░ └───┘ ░ └──────────┘

>>> ansatz = RealAmplitudes(4, reps=1, entanglement='circular', insert_barriers=True)
>>> print(ansatz)
┌──────────┐ ░ ┌───┐ ░ ┌──────────┐
q_0: ┤ RY(θ[0]) ├─░─┤ X ├──■─────────────░─┤ RY(θ[4]) ├
├──────────┤ ░ └─┬─┘┌─┴─┐ ░ ├──────────┤
q_1: ┤ RY(θ[1]) ├─░───┼──┤ X ├──■────────░─┤ RY(θ[5]) ├
├──────────┤ ░ │ └───┘┌─┴─┐ ░ ├──────────┤
q_2: ┤ RY(θ[2]) ├─░───┼───────┤ X ├──■───░─┤ RY(θ[6]) ├
├──────────┤ ░ │ └───┘┌─┴─┐ ░ ├──────────┤
q_3: ┤ RY(θ[3]) ├─░───■────────────┤ X ├─░─┤ RY(θ[7]) ├
└──────────┘ ░ └───┘ ░ └──────────┘

>>> ansatz = RealAmplitudes(4, reps=2, entanglement=[[0,3], [0,2]],
... skip_unentangled_qubits=True)
>>> print(ansatz)
┌──────────┐ ┌──────────┐ ┌──────────┐
q_0: ┤ RY(θ[0]) ├──■───────■──────┤ RY(θ[3]) ├──■───────■──────┤ RY(θ[6]) ├
└──────────┘ │ │ └──────────┘ │ │ └──────────┘
q_1: ──────────────┼───────┼────────────────────┼───────┼──────────────────
┌──────────┐ │ ┌─┴─┐ ┌──────────┐ │ ┌─┴─┐ ┌──────────┐
q_2: ┤ RY(θ[1]) ├──┼─────┤ X ├────┤ RY(θ[4]) ├──┼─────┤ X ├────┤ RY(θ[7]) ├
├──────────┤┌─┴─┐┌──┴───┴───┐└──────────┘┌─┴─┐┌──┴───┴───┐└──────────┘
q_3: ┤ RY(θ[2]) ├┤ X ├┤ RY(θ[5]) ├────────────┤ X ├┤ RY(θ[8]) ├────────────
└──────────┘└───┘└──────────┘ └───┘└──────────┘

"""

def __init__(
Expand Down Expand Up @@ -125,8 +51,6 @@ def __init__(
Default to 'reverse_linear' entanglement.
Note that 'reverse_linear' entanglement provides the same unitary as 'full'
with fewer entangling gates.
See the Examples section of :class:`~qiskit.circuit.library.TwoLocal` for more
detail.
initial_state: A `QuantumCircuit` object to prepend to the circuit.
skip_unentangled_qubits: If True, the single qubit gates are only applied to qubits
that are entangled with another qubit. If False, the single qubit gates are applied
Expand All @@ -136,17 +60,16 @@ def __init__(
to each qubit in the Ansatz. Defaults to False.
skip_final_rotation_layer: If False, a rotation layer is added at the end of the
ansatz. If True, no rotation layer is added.
parameter_prefix: The parameterized gates require a parameter to be defined, for which
we use :class:`~qiskit.circuit.ParameterVector`.
parameter_prefix: The parameterized gates require a parameter to be defined
insert_barriers: If True, barriers are inserted in between each layer. If False,
no barriers are inserted.

"""
super().__init__(
num_qubits=num_qubits,
reps=reps,
rotation_blocks=RYGate,
entanglement_blocks=CXGate,
rotation_blocks=RY,
entanglement_blocks=CNOT,
entanglement=entanglement,
initial_state=initial_state,
skip_unentangled_qubits=skip_unentangled_qubits,
Expand Down