TwoLocal¶
- 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
orCXGate
) 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 theparameter_prefix
.Construct a new two-local circuit.
- Parameters
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.
Examples
>>> 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 ├ └───┘ ░ └───┘ ░ └───┘ ░ └───┘ ░ └───┘
Attributes
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 initial state that is added in front of the n-local circuit.
If barriers are inserted in between the layers or not.
Deprecated, use
num_qubits
instead.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.
Methods
TwoLocal.AND
(qr_variables, qb_target, …[, …])Build a collective conjunction (AND) circuit in place using mct.
TwoLocal.OR
(qr_variables, qb_target, qr_ancillae)Build a collective disjunction (OR) circuit in place using mct.
TwoLocal.__getitem__
(item)Return indexed operation.
Return number of operations in circuit.
TwoLocal.add_layer
(other[, entanglement, front])Append another layer to the NLocal.
TwoLocal.add_register
(*regs)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.
TwoLocal.barrier
(*qargs)Apply
Barrier
.TwoLocal.bind_parameters
(value_dict)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
.TwoLocal.ch
(control_qubit, 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
.TwoLocal.combine
(rhs)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.copy
([name])Copy the circuit.
Count each operation kind in the circuit.
TwoLocal.crx
(theta, control_qubit, …[, …])Apply
CRXGate
.TwoLocal.cry
(theta, control_qubit, …[, …])Apply
CRYGate
.TwoLocal.crz
(theta, control_qubit, …[, …])Apply
CRZGate
.TwoLocal.cswap
(control_qubit, target_qubit1, …)Apply
CSwapGate
.TwoLocal.cu1
(theta, control_qubit, …[, …])Apply
CU1Gate
.TwoLocal.cu3
(theta, phi, lam, control_qubit, …)Apply
CU3Gate
.TwoLocal.cx
(control_qubit, target_qubit, *)Apply
CXGate
.TwoLocal.cy
(control_qubit, target_qubit, *)Apply
CYGate
.TwoLocal.cz
(control_qubit, target_qubit, *)Apply
CZGate
.TwoLocal.dcx
(qubit1, qubit2)Apply
DCXGate
.Call a decomposition pass on this circuit, to decompose one level (shallow decompose).
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.
TwoLocal.extend
(rhs)Append QuantumCircuit to the right hand side if it contains compatible registers.
TwoLocal.fredkin
(control_qubit, …[, ctl, …])Apply
CSwapGate
.TwoLocal.from_qasm_file
(path)Take in a QASM file and generate a QuantumCircuit object.
TwoLocal.from_qasm_str
(qasm_str)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.
TwoLocal.h
(qubit, *[, q])Apply
HGate
.TwoLocal.hamiltonian
(operator, time, qubits)Apply hamiltonian evolution to to qubits.
TwoLocal.has_register
(register)Test if this circuit has the register r.
TwoLocal.i
(qubit, *[, q])Apply
IGate
.TwoLocal.id
(qubit, *[, q])Apply
IGate
.TwoLocal.iden
(qubit, *[, q])Deprecated identity gate.
TwoLocal.initialize
(params, qubits)Apply initialize to circuit.
Invert 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.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).
TwoLocal.measure_active
([inplace])Adds measurement to all non-idle qubits.
TwoLocal.measure_all
([inplace])Adds measurement to all qubits.
Mirror the circuit by reversing the instructions.
TwoLocal.ms
(theta, qubits)Apply
MSGate
.TwoLocal.num_connected_components
([unitary_only])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.
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.
TwoLocal.r
(theta, phi, qubit, *[, q])Apply
RGate
.TwoLocal.rcccx
(control_qubit1, …)Apply
RC3XGate
.TwoLocal.rccx
(control_qubit1, …)Apply
RCCXGate
.TwoLocal.remove_final_measurements
([inplace])Removes final measurement on all qubits if they are present.
TwoLocal.reset
(qubit)Reset q.
TwoLocal.rx
(theta, qubit, *[, label, q])Apply
RXGate
.TwoLocal.rxx
(theta, qubit1, qubit2)Apply
RXXGate
.TwoLocal.ry
(theta, qubit, *[, label, q])Apply
RYGate
.TwoLocal.ryy
(theta, qubit1, qubit2)Apply
RYYGate
.TwoLocal.rz
(phi, qubit, *[, q])Apply
RZGate
.TwoLocal.rzx
(theta, qubit1, qubit2)Apply
RZXGate
.TwoLocal.rzz
(theta, qubit1, qubit2)Apply
RZZGate
.TwoLocal.s
(qubit, *[, q])Apply
SGate
.TwoLocal.sdg
(qubit, *[, q])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.
TwoLocal.snapshot_stabilizer
(label)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
.TwoLocal.t
(qubit, *[, q])Apply
TGate
.TwoLocal.tdg
(qubit, *[, q])Apply
TdgGate
.TwoLocal.to_gate
([parameter_map])Create a Gate out of this circuit.
TwoLocal.to_instruction
([parameter_map])Create an Instruction out of this circuit.
TwoLocal.toffoli
(control_qubit1, …[, …])Apply
CCXGate
.TwoLocal.u1
(theta, qubit, *[, q])Apply
U1Gate
.TwoLocal.u2
(phi, lam, qubit, *[, q])Apply
U2Gate
.TwoLocal.u3
(theta, phi, lam, qubit, *[, q])Apply
U3Gate
.TwoLocal.uc
(gate_list, q_controls, q_target)Attach a uniformly controlled gates (also called multiplexed gates) to a circuit.
TwoLocal.ucg
(angle_list, q_controls, q_target)Deprecated version of uc.
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.ucx
(angle_list, q_controls, q_target)Deprecated version of ucrx.
TwoLocal.ucy
(angle_list, q_controls, q_target)Deprecated version of ucry.
TwoLocal.ucz
(angle_list, q_controls, q_target)Deprecated version of ucrz.
TwoLocal.unitary
(obj, qubits[, label])Apply unitary gate to q.
Return number of qubits plus clbits in circuit.
TwoLocal.x
(qubit, *[, label, ctrl_state, q])Apply
XGate
.TwoLocal.y
(qubit, *[, q])Apply
YGate
.TwoLocal.z
(qubit, *[, q])Apply
ZGate
.TwoLocal.__getitem__
(item)Return indexed operation.
Return number of operations in circuit.