class 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)[source]

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 \(i\) entangled with qubit \(i + 1\), for all \(i \in \{0, 1, ... , n - 2\}\), where \(n\) is the total number of qubits.

  • '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.


>>> two = TwoLocal(3, 'ry', 'cx', 'linear', reps=2, insert_barriers=True)
>>> print(two)  # 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)
>>> qc = QuantumCircuit(3)
>>> qc += two
>>> print(qc.decompose().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)
>>> 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)
>>> circuit = two + two
>>> print(circuit.decompose().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)
>>> print(two)
     ┌───┐ ░            ░ ┌───┐ ░       ░ ┌───┐
q_0: ┤ X ├─░───■────■───░─┤ X ├─░───────░─┤ X ├
     ├───┤ ░ ┌─┴─┐  │   ░ ├───┤ ░       ░ ├───┤
q_1: ┤ X ├─░─┤ X ├──┼───░─┤ X ├─░───■───░─┤ X ├
     ├───┤ ░ └───┘┌─┴─┐ ░ ├───┤ ░ ┌─┴─┐ ░ ├───┤
q_2: ┤ X ├─░──────┤ X ├─░─┤ X ├─░─┤ X ├─░─┤ X ├
     └───┘ ░      └───┘ ░ └───┘ ░ └───┘ ░ └───┘

Construct a new two-local circuit.

  • num_qubits (Optional[int]) – The number of qubits of the two-local circuit.

  • rotation_blocks (Union[str, List[str], type, List[type], QuantumCircuit, List[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 (Union[str, List[str], type, List[type], QuantumCircuit, List[QuantumCircuit], None]) – The gates used in the entanglement layer. Can be specified in the same format as rotation_blocks.

  • entanglement (Union[str, List[List[int]], Callable[[int], List[int]]]) – Specifies the entanglement structure. Can be a string (‘full’, ‘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. 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. If False, the single qubit gates are applied to each qubit in the Ansatz. Defaults to False.

  • skip_final_rotation_layer (bool) – If False, a rotation layer is added at the end of the ansatz. If True, no rotation layer is added.

  • parameter_prefix (str) – The parameterized gates require a parameter to be defined, for which we use instances of qiskit.circuit.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. If False, no barriers are inserted. Defaults to False.

  • initial_state (Optional[Any]) – An InitialState object to prepend to the circuit.



Returns a list of ancilla bits in the order that the registers were added.


Return calibration dictionary.


Returns a list of classical bits in the order that the registers were added.

Return the circuit data (instructions and context).


Get the entanglement strategy.


The blocks in the entanglement layers.



Return the global phase of the circuit in radians.



Return the initial state that is added in front of the n-local circuit.


If barriers are inserted in between the layers or not.



Return the number of ancilla qubits.


Return number of classical bits.


Return the number of layers in the n-local circuit.


Convenience function to get the number of parameter objects in the circuit.


The number of total parameters that can be set to distinct values.


Returns the number of qubits in this circuit.


The parameters used in the underlying circuit.


The parameter bounds for the unbound parameters in the circuit.


Get the Parameter objects in the circuit.


The initial points for the parameters.



A list of the quantum registers associated with the circuit.


Returns a list of quantum bits in the order that the registers were added.


The number of times rotation and entanglement block are repeated.


The blocks in the rotation layers.



Return indexed operation.


Return number of operations in circuit.

TwoLocal.add_calibration(gate, qubits, schedule)

Register a low-level, custom pulse definition for the given gate.

TwoLocal.add_layer(other[, entanglement, front])

Append another layer to the NLocal.


Add registers.

TwoLocal.append(instruction[, qargs, cargs])

Append one or more instructions to the end of the circuit, modifying the circuit in place.

TwoLocal.assign_parameters(param_dict[, inplace])

Assign parameters to the n-local circuit.


Apply Barrier.


Assign numeric parameters to values yielding a new circuit.

TwoLocal.cast(value, _type)

Best effort to cast value to type.


Converts several classical bit representations (such as indexes, range, etc.) into a list of classical bits.

TwoLocal.ccx(control_qubit1, control_qubit2, …)

Apply CCXGate., target_qubit[, …])

Apply CHGate.


Return the current number of instances of this class, useful for auto naming.


Return the prefix to use for auto naming.

TwoLocal.cnot(control_qubit, target_qubit[, …])

Apply CXGate.


Append rhs to self if self contains compatible registers.

TwoLocal.compose(other[, qubits, clbits, …])

Compose circuit with other circuit or instruction, optionally permuting wires.

TwoLocal.control([num_ctrl_qubits, label, …])

Control this circuit on num_ctrl_qubits qubits.


Copy the circuit.


Count each operation kind in the circuit.

TwoLocal.cp(theta, control_qubit, target_qubit)

Apply CPhaseGate.

TwoLocal.crx(theta, control_qubit, target_qubit)

Apply CRXGate.

TwoLocal.cry(theta, control_qubit, target_qubit)

Apply CRYGate.

TwoLocal.crz(theta, control_qubit, target_qubit)

Apply CRZGate.

TwoLocal.cswap(control_qubit, target_qubit1, …)

Apply CSwapGate.

TwoLocal.csx(control_qubit, target_qubit[, …])

Apply CSXGate., phi, lam, gamma, …[, …])

Apply CUGate.

TwoLocal.cu1(theta, control_qubit, target_qubit)

Apply CU1Gate.

TwoLocal.cu3(theta, phi, lam, control_qubit, …)

Apply CU3Gate., target_qubit[, …])

Apply CXGate., target_qubit[, …])

Apply CYGate., target_qubit[, …])

Apply CZGate.

TwoLocal.dcx(qubit1, qubit2)

Apply DCXGate.


Call a decomposition pass on this circuit, to decompose one level (shallow decompose).

TwoLocal.delay(duration[, qarg, unit])

Apply Delay.


Return circuit depth (i.e., length of critical path).

TwoLocal.diag_gate(diag, qubit)

Deprecated version of QuantumCircuit.diagonal.

TwoLocal.diagonal(diag, qubit)

Attach a diagonal gate to a circuit.

TwoLocal.draw([output, scale, filename, …])

Draw the quantum circuit.


Append QuantumCircuit to the right hand side if it contains compatible registers.

TwoLocal.fredkin(control_qubit, …)

Apply CSwapGate.


Take in a QASM file and generate a QuantumCircuit object.


Take in a QASM string and generate a QuantumCircuit object.

TwoLocal.get_entangler_map(rep_num, …)

Overloading to handle the special case of 1 qubit where the entanglement are ignored.


Get the indices of unentangled qubits in a set.


Apply HGate.

TwoLocal.hamiltonian(operator, time, qubits)

Apply hamiltonian evolution to to qubits.


Test if this circuit has the register r.


Apply IGate.

Apply IGate.

TwoLocal.initialize(params, qubits)

Apply initialize to circuit.


Invert (take adjoint of) this circuit.

TwoLocal.iso(isometry, q_input, …[, …])

Attach an arbitrary isometry from m to n qubits to a circuit.

TwoLocal.isometry(isometry, q_input, …[, …])

Attach an arbitrary isometry from m to n qubits to a circuit.

TwoLocal.iswap(qubit1, qubit2)

Apply iSwapGate.

TwoLocal.mcmt(gate, control_qubits, …[, …])

Apply a multi-control, multi-target using a generic gate.

TwoLocal.mcp(lam, control_qubits, target_qubit)

Apply MCPhaseGate.

TwoLocal.mcrx(theta, q_controls, q_target[, …])

Apply Multiple-Controlled X rotation gate

TwoLocal.mcry(theta, q_controls, q_target, …)

Apply Multiple-Controlled Y rotation gate

TwoLocal.mcrz(lam, q_controls, q_target[, …])

Apply Multiple-Controlled Z rotation gate

TwoLocal.mct(control_qubits, target_qubit[, …])

Apply MCXGate.

TwoLocal.mcu1(lam, control_qubits, target_qubit)

Apply MCU1Gate.

TwoLocal.mcx(control_qubits, target_qubit[, …])

Apply MCXGate.

TwoLocal.measure(qubit, cbit)

Measure quantum bit into classical bit (tuples).


Adds measurement to all non-idle qubits.


Adds measurement to all qubits.


DEPRECATED: use circuit.reverse_ops()., qubits)

Apply MSGate.


How many non-entangled subcircuits can the circuit be factored to.


Return number of non-local gates (i.e.


Computes the number of tensor factors in the unitary (quantum) part of the circuit only.


Computes the number of tensor factors in the unitary (quantum) part of the circuit only.

TwoLocal.p(theta, qubit)

Apply PhaseGate.

TwoLocal.power(power[, matrix_power])

Raise this circuit to the power of power.


Returns information about the setting.

TwoLocal.qasm([formatted, filename])

Return OpenQASM string.


Converts several qubit representations (such as indexes, range, etc.) into a list of qubits.


Return the duration between the start and stop time of the first and last instructions, excluding delays, over the supplied qubits.


Return the start time of the first instruction, excluding delays, over the supplied qubits.


Return the stop time of the last instruction, excluding delays, over the supplied qubits.

TwoLocal.r(theta, phi, qubit)

Apply RGate.

TwoLocal.rcccx(control_qubit1, …)

Apply RC3XGate.

TwoLocal.rccx(control_qubit1, …)

Apply RCCXGate.


Removes final measurement on all qubits if they are present.


Repeat this circuit reps times.


Reset q.


Return a circuit with the opposite order of wires.


Reverse the circuit by reversing the order of instructions.

TwoLocal.rx(theta, qubit[, label])

Apply RXGate.

TwoLocal.rxx(theta, qubit1, qubit2)

Apply RXXGate.

TwoLocal.ry(theta, qubit[, label])

Apply RYGate.

TwoLocal.ryy(theta, qubit1, qubit2)

Apply RYYGate.

TwoLocal.rz(phi, qubit)

Apply RZGate.

TwoLocal.rzx(theta, qubit1, qubit2)

Apply RZXGate.

TwoLocal.rzz(theta, qubit1, qubit2)

Apply RZZGate.


Apply SGate.


Apply SdgGate.


Returns total number of gate operations in circuit.

TwoLocal.snapshot(label[, snapshot_type, …])

Take a statevector snapshot of the internal simulator representation.

TwoLocal.snapshot_density_matrix(label[, qubits])

Take a density matrix snapshot of simulator state.

TwoLocal.snapshot_expectation_value(label, …)

Take a snapshot of expectation value <O> of an Operator.

TwoLocal.snapshot_probabilities(label, qubits)

Take a probability snapshot of the simulator state.


Take a stabilizer snapshot of the simulator state.


Take a statevector snapshot of the simulator state.

TwoLocal.squ(unitary_matrix, qubit[, mode, …])

Decompose an arbitrary 2*2 unitary into three rotation gates.

TwoLocal.swap(qubit1, qubit2)

Apply SwapGate.

Apply SXGate.


Apply SXdgGate.


Apply TGate.


Apply TdgGate.

TwoLocal.to_gate([parameter_map, label])

Create a Gate out of this circuit.


Create an Instruction out of this circuit.

TwoLocal.toffoli(control_qubit1, …)

Apply CCXGate.

TwoLocal.u(theta, phi, lam, qubit)

Apply UGate.

TwoLocal.u1(theta, qubit)

Apply U1Gate.

TwoLocal.u2(phi, lam, qubit)

Apply U2Gate.

TwoLocal.u3(theta, phi, lam, qubit)

Apply U3Gate.

TwoLocal.uc(gate_list, q_controls, q_target)

Attach a uniformly controlled gates (also called multiplexed gates) to a circuit.

TwoLocal.ucrx(angle_list, q_controls, q_target)

Attach a uniformly controlled (also called multiplexed) Rx rotation gate to a circuit.

TwoLocal.ucry(angle_list, q_controls, q_target)

Attach a uniformly controlled (also called multiplexed) Ry rotation gate to a circuit.

TwoLocal.ucrz(angle_list, q_controls, q_target)

Attach a uniformly controlled (also called multiplexed gates) Rz rotation gate to a circuit.

TwoLocal.unitary(obj, qubits[, label])

Apply unitary gate to q.


Return number of qubits plus clbits in circuit.

TwoLocal.x(qubit[, label])

Apply XGate.


Apply YGate.


Apply ZGate.


Return indexed operation.


Return number of operations in circuit.