TwoLocal
class qiskit.circuit.library.TwoLocal(num_qubits=None, rotation_blocks=None, entanglement_blocks=None, entanglement='full', reps=3, skip_unentangled_qubits=False, skip_final_rotation_layer=False, parameter_prefix='θ', insert_barriers=False, initial_state=None, name='TwoLocal', flatten=None)
Bases: NLocal
The two-local circuit.
The two-local circuit is a parameterized circuit consisting of alternating rotation layers and entanglement layers. The rotation layers are single qubit gates applied on all qubits. The entanglement layer uses two-qubit gates to entangle the qubits according to a strategy set using entanglement
. Both the rotation and entanglement gates can be specified as string (e.g. 'ry'
or 'cx'
), as gate-type (e.g. RYGate
or CXGate
) or as QuantumCircuit (e.g. a 1-qubit circuit or 2-qubit circuit).
A set of default entanglement strategies is provided:
'full'
entanglement is each qubit is entangled with all the others.'linear'
entanglement is qubit entangled with qubit , for all , where is the total number of qubits.'reverse_linear'
entanglement is qubit entangled with qubit , for all , where is the total number of qubits. Note that ifentanglement_blocks = 'cx'
then this option provides the same unitary as'full'
with fewer entangling gates.'pairwise'
entanglement is one layer where qubit is entangled with qubit , for all even values of , and then a second layer where qubit is entangled with qubit , for all odd values of .'circular'
entanglement is linear entanglement but with an additional entanglement of the first and last qubit before the linear part.'sca'
(shifted-circular-alternating) entanglement is a generalized and modified version of the proposed circuit 14 in Sim et al.. It consists of circular entanglement where the ‘long’ entanglement connecting the first with the last qubit is shifted by one each block. Furthermore the role of control and target qubits are swapped every block (therefore alternating).
The entanglement can further be specified using an entangler map, which is a list of index pairs, such as
>>> entangler_map = [(0, 1), (1, 2), (2, 0)]
If different entanglements per block should be used, provide a list of entangler maps. See the examples below on how this can be used.
>>> entanglement = [entangler_map_layer_1, entangler_map_layer_2, ... ]
Barriers can be inserted in between the different layers for better visualization using the insert_barriers
attribute.
For each parameterized gate a new parameter is generated using a ParameterVector
. The name of these parameters can be chosen using the parameter_prefix
.
Examples
>>> two = TwoLocal(3, 'ry', 'cx', 'linear', reps=2, insert_barriers=True)
>>> print(two.decompose()) # decompose the layers into standard gates
┌──────────┐ ░ ░ ┌──────────┐ ░ ░ ┌──────────┐
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]) ├
└──────────┘ ░ └───┘ ░ └──────────┘ ░ └───┘ ░ └──────────┘
>>> two = TwoLocal(3, ['ry','rz'], 'cz', 'full', reps=1, insert_barriers=True, flatten=True)
>>> qc = QuantumCircuit(3)
>>> qc &= two
>>> print(qc.draw())
┌──────────┐┌──────────┐ ░ ░ ┌──────────┐ ┌──────────┐
q_0: ┤ Ry(θ[0]) ├┤ Rz(θ[3]) ├─░──■──■─────░─┤ Ry(θ[6]) ├─┤ Rz(θ[9]) ├
├──────────┤├──────────┤ ░ │ │ ░ ├──────────┤┌┴──────────┤
q_1: ┤ Ry(θ[1]) ├┤ Rz(θ[4]) ├─░──■──┼──■──░─┤ Ry(θ[7]) ├┤ Rz(θ[10]) ├
├──────────┤├──────────┤ ░ │ │ ░ ├──────────┤├───────────┤
q_2: ┤ Ry(θ[2]) ├┤ Rz(θ[5]) ├─░─────■──■──░─┤ Ry(θ[8]) ├┤ Rz(θ[11]) ├
└──────────┘└──────────┘ ░ ░ └──────────┘└───────────┘
>>> entangler_map = [[0, 1], [1, 2], [2, 0]] # circular entanglement for 3 qubits
>>> two = TwoLocal(3, 'x', 'crx', entangler_map, reps=1, flatten=True)
>>> print(two) # note: no barriers inserted this time!
┌───┐ ┌──────────┐┌───┐
q_0: |0>┤ X ├─────■───────────────────────┤ Rx(θ[2]) ├┤ X ├
├───┤┌────┴─────┐ ┌───┐└─────┬────┘└───┘
q_1: |0>┤ X ├┤ Rx(θ[0]) ├─────■──────┤ X ├──────┼──────────
├───┤└──────────┘┌────┴─────┐└───┘ │ ┌───┐
q_2: |0>┤ X ├────────────┤ Rx(θ[1]) ├───────────■─────┤ X ├
└───┘ └──────────┘ └───┘
>>> entangler_map = [[0, 3], [0, 2]] # entangle the first and last two-way
>>> two = TwoLocal(4, [], 'cry', entangler_map, reps=1, flatten=True)
>>> circuit = two.compose(two)
>>> print(circuit.draw()) # note, that the parameters are the same!
q_0: ─────■───────────■───────────■───────────■──────
│ │ │ │
q_1: ─────┼───────────┼───────────┼───────────┼──────
│ ┌────┴─────┐ │ ┌────┴─────┐
q_2: ─────┼──────┤ Ry(θ[1]) ├─────┼──────┤ Ry(θ[1]) ├
┌────┴─────┐└──────────┘┌────┴─────┐└──────────┘
q_3: ┤ Ry(θ[0]) ├────────────┤ Ry(θ[0]) ├────────────
└──────────┘ └──────────┘
>>> layer_1 = [(0, 1), (0, 2)]
>>> layer_2 = [(1, 2)]
>>> two = TwoLocal(3, 'x', 'cx', [layer_1, layer_2], reps=2, insert_barriers=True,
... flatten=True)
>>> print(two)
┌───┐ ░ ░ ┌───┐ ░ ░ ┌───┐
q_0: ┤ X ├─░───■────■───░─┤ X ├─░───────░─┤ X ├
├───┤ ░ ┌─┴─┐ │ ░ ├───┤ ░ ░ ├───┤
q_1: ┤ X ├─░─┤ X ├──┼───░─┤ X ├─░───■───░─┤ X ├
├───┤ ░ └───┘┌─┴─┐ ░ ├───┤ ░ ┌─┴─┐ ░ ├───┤
q_2: ┤ X ├─░──────┤ X ├─░─┤ X ├─░─┤ X ├─░─┤ X ├
└───┘ ░ └───┘ ░ └───┘ ░ └───┘ ░ └───┘
The class qiskit.circuit.library.n_local.two_local.TwoLocal
is deprecated as of Qiskit 2.1. It will be removed in Qiskit 3.0. Use the function qiskit.circuit.library.n_local instead.
Parameters
- num_qubits (int | None) – The number of qubits of the two-local circuit.
- rotation_blocks (str |type |qiskit.circuit.Instruction |QuantumCircuit |list[str |type |qiskit.circuit.Instruction |QuantumCircuit] | None) – The gates used in the rotation layer. Can be specified via the name of a gate (e.g.
'ry'
) or the gate type itself (e.g.RYGate
). If only one gate is provided, the gate same gate is applied to each qubit. If a list of gates is provided, all gates are applied to each qubit in the provided order. See the Examples section for more detail. - entanglement_blocks (str |type |qiskit.circuit.Instruction |QuantumCircuit |list[str |type |qiskit.circuit.Instruction |QuantumCircuit] | None) – The gates used in the entanglement layer. Can be specified in the same format as
rotation_blocks
. - entanglement (str |list[list[int]] | Callable[[int], list[int]]) – Specifies the entanglement structure. Can be a string (
'full'
,'linear'
,'reverse_linear'
,'circular'
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. Default to'full'
entanglement. Note that ifentanglement_blocks = 'cx'
, then'full'
entanglement provides the same unitary as'reverse_linear'
but the latter option has fewer entangling gates. See the Examples section for more detail. - reps (int) – Specifies how often a block consisting of a rotation layer and entanglement layer is repeated.
- skip_unentangled_qubits (bool) – If
True
, the single qubit gates are only applied to qubits that are entangled with another qubit. IfFalse
, the single qubit gates are applied to each qubit in the ansatz. Defaults toFalse
. - skip_final_rotation_layer (bool) – If
False
, a rotation layer is added at the end of the ansatz. IfTrue
, no rotation layer is added. - parameter_prefix (str) – The parameterized gates require a parameter to be defined, for which we use instances of
Parameter
. The name of each parameter will be this specified prefix plus its index. - insert_barriers (bool) – If
True
, barriers are inserted in between each layer. IfFalse
, no barriers are inserted. Defaults toFalse
. - initial_state (QuantumCircuit | None) – A
QuantumCircuit
object to prepend to the circuit. - flatten (bool | None) – 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 toTrue
to avoid a large performance overhead for parameter binding. - name (str) –
Attributes
name
Type: str
A human-readable name for the circuit.
Example
from qiskit import QuantumCircuit
qc = QuantumCircuit(2, 2, name="my_circuit")
print(qc.name)
my_circuit