C贸digo fuente para qiskit.circuit.library.n_local.excitation_preserving

# This code is part of Qiskit.
#
# (C) Copyright IBM 2017, 2020.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

"""The ExcitationPreserving 2-local circuit."""

from __future__ import annotations
from collections.abc import Callable
from numpy import pi

from qiskit.circuit import QuantumCircuit, Parameter
from qiskit.circuit.library.standard_gates import RZGate
from .two_local import TwoLocal


[documentos]class ExcitationPreserving(TwoLocal): r"""The heuristic excitation-preserving wave function ansatz. The ``ExcitationPreserving`` circuit preserves the ratio of :math:`|00\rangle`, :math:`|01\rangle + |10\rangle` and :math:`|11\rangle` states. To this end, this circuit uses two-qubit interactions of the form .. math:: \newcommand{\th}{\theta/2} \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & \cos\left(\th\right) & -i\sin\left(\th\right) & 0 \\ 0 & -i\sin\left(\th\right) & \cos\left(\th\right) & 0 \\ 0 & 0 & 0 & e^{-i\phi} \end{pmatrix} for the mode ``'fsim'`` or with :math:`e^{-i\phi} = 1` for the mode ``'iswap'``. Note that other wave functions, such as UCC-ansatzes, are also excitation preserving. However these can become complex quickly, while this heuristically motivated circuit follows a simpler pattern. This trial wave function consists of layers of :math:`Z` rotations with 2-qubit entanglements. The entangling is creating using :math:`XX+YY` rotations and optionally a controlled-phase gate for the mode ``'fsim'``. See :class:`~qiskit.circuit.library.RealAmplitudes` for more detail on the possible arguments and options such as skipping unentanglement qubits, which apply here too. The rotations of the ExcitationPreserving ansatz can be written as Examples: >>> ansatz = ExcitationPreserving(3, reps=1, insert_barriers=True, entanglement='linear') >>> print(ansatz) # show the circuit 鈹屸攢鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹 鈻 鈹屸攢鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹愨攲鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹 鈻 鈹屸攢鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹 q_0: 鈹 RZ(胃[0]) 鈹溾攢鈻戔攢鈹0 鈹溾敜0 鈹溾攢鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈻戔攢鈹 RZ(胃[5]) 鈹 鈹溾攢鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹 鈻 鈹 RXX(胃[3]) 鈹傗攤 RYY(胃[3]) 鈹傗攲鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹愨攲鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹 鈻 鈹溾攢鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹 q_1: 鈹 RZ(胃[1]) 鈹溾攢鈻戔攢鈹1 鈹溾敜1 鈹溾敜0 鈹溾敜0 鈹溾攢鈻戔攢鈹 RZ(胃[6]) 鈹 鈹溾攢鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹 鈻 鈹斺攢鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹樷敂鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹樷攤 RXX(胃[4]) 鈹傗攤 RYY(胃[4]) 鈹 鈻 鈹溾攢鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹 q_2: 鈹 RZ(胃[2]) 鈹溾攢鈻戔攢鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹1 鈹溾敜1 鈹溾攢鈻戔攢鈹 RZ(胃[7]) 鈹 鈹斺攢鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹 鈻 鈹斺攢鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹樷敂鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹 鈻 鈹斺攢鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹 >>> ansatz = ExcitationPreserving(2, reps=1) >>> qc = QuantumCircuit(2) # create a circuit and append the RY variational form >>> qc.cry(0.2, 0, 1) # do some previous operation >>> qc.compose(ansatz, inplace=True) # add the swaprz >>> qc.draw() 鈹屸攢鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹愨攲鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹愨攲鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹愨攲鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹 q_0: 鈹鈹鈹鈹鈹鈻犫攢鈹鈹鈹鈹鈹 RZ(胃[0]) 鈹溾敜0 鈹溾敜0 鈹溾敜 RZ(胃[3]) 鈹 鈹屸攢鈹鈹鈹鈹粹攢鈹鈹鈹鈹愨敎鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹も攤 RXX(胃[2]) 鈹傗攤 RYY(胃[2]) 鈹傗敎鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹 q_1: 鈹 RY(0.2) 鈹溾敜 RZ(胃[1]) 鈹溾敜1 鈹溾敜1 鈹溾敜 RZ(胃[4]) 鈹 鈹斺攢鈹鈹鈹鈹鈹鈹鈹鈹鈹樷敂鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹樷敂鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹樷敂鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹樷敂鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹 >>> ansatz = ExcitationPreserving(3, reps=1, mode='fsim', entanglement=[[0,2]], ... insert_barriers=True) >>> print(ansatz) 鈹屸攢鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹 鈻 鈹屸攢鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹愨攲鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹 鈻 鈹屸攢鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹 q_0: 鈹 RZ(胃[0]) 鈹溾攢鈻戔攢鈹0 鈹溾敜0 鈹溾攢鈻犫攢鈹鈹鈹鈹鈹鈻戔攢鈹 RZ(胃[5]) 鈹 鈹溾攢鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹 鈻 鈹 鈹傗攤 鈹 鈹 鈻 鈹溾攢鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹 q_1: 鈹 RZ(胃[1]) 鈹溾攢鈻戔攢鈹 RXX(胃[3]) 鈹溾敜 RYY(胃[3]) 鈹溾攢鈹尖攢鈹鈹鈹鈹鈹鈻戔攢鈹 RZ(胃[6]) 鈹 鈹溾攢鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹 鈻 鈹 鈹傗攤 鈹 鈹偽竅4] 鈻 鈹溾攢鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹 q_2: 鈹 RZ(胃[2]) 鈹溾攢鈻戔攢鈹1 鈹溾敜1 鈹溾攢鈻犫攢鈹鈹鈹鈹鈹鈻戔攢鈹 RZ(胃[7]) 鈹 鈹斺攢鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹 鈻 鈹斺攢鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹樷敂鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹 鈻 鈹斺攢鈹鈹鈹鈹鈹鈹鈹鈹鈹鈹 """ def __init__( self, num_qubits: int | None = None, mode: str = "iswap", entanglement: str | list[list[int]] | Callable[[int], list[int]] = "full", reps: int = 3, skip_unentangled_qubits: bool = False, skip_final_rotation_layer: bool = False, parameter_prefix: str = "胃", insert_barriers: bool = False, initial_state: QuantumCircuit | None = None, name: str = "ExcitationPreserving", flatten: bool | None = None, ) -> None: """ Args: num_qubits: The number of qubits of the ExcitationPreserving circuit. mode: Choose the entangler mode, can be `'iswap'` or `'fsim'`. reps: Specifies how often the structure of a rotation layer followed by an entanglement layer is repeated. entanglement: Specifies the entanglement structure. Can be a string ('full', 'linear' or 'sca'), a list of integer-pairs specifying the indices of qubits entangled with one another, or a callable returning such a list provided with the index of the entanglement layer. 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 to each qubit in the Ansatz. Defaults to False. 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 to each qubit in the Ansatz. Defaults to False. skip_final_rotation_layer: If True, a rotation layer is added at the end of the ansatz. If False, no rotation layer is added. Defaults to True. parameter_prefix: The parameterized gates require a parameter to be defined, for which we use :class:`~qiskit.circuit.ParameterVector`. insert_barriers: If True, barriers are inserted in between each layer. If False, no barriers are inserted. flatten: Set this to ``True`` to output a flat circuit instead of nesting it inside multiple layers of gate objects. By default currently the contents of the output circuit will be wrapped in nested objects for cleaner visualization. However, if you're using this circuit for anything besides visualization its **strongly** recommended to set this flag to ``True`` to avoid a large performance overhead for parameter binding. Raises: ValueError: If the selected mode is not supported. """ supported_modes = ["iswap", "fsim"] if mode not in supported_modes: raise ValueError(f"Unsupported mode {mode}, choose one of {supported_modes}") theta = Parameter("胃") swap = QuantumCircuit(2, name="Interaction") swap.rxx(theta, 0, 1) swap.ryy(theta, 0, 1) if mode == "fsim": phi = Parameter("蠁") swap.cp(phi, 0, 1) super().__init__( num_qubits=num_qubits, rotation_blocks=RZGate, entanglement_blocks=swap, entanglement=entanglement, reps=reps, skip_unentangled_qubits=skip_unentangled_qubits, skip_final_rotation_layer=skip_final_rotation_layer, parameter_prefix=parameter_prefix, insert_barriers=insert_barriers, initial_state=initial_state, name=name, flatten=flatten, ) @property def parameter_bounds(self) -> list[tuple[float, float]]: """Return the parameter bounds. Returns: The parameter bounds. """ return self.num_parameters * [(-pi, pi)]