RealAmplitudes

class RealAmplitudes(num_qubits=None, entanglement='full', reps=3, skip_unentangled_qubits=False, skip_final_rotation_layer=False, parameter_prefix='θ', insert_barriers=False, initial_state=None)[source]

The RealAmplitudes 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 \(Y\) rotations and \(CX\) entanglements. The entanglement pattern can be user-defined or selected from a predefined set.

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

┌──────────┐ ░                 ░ ┌──────────┐ ░                 ░ ┌──────────┐
┤ RY(θ[0]) ├─░───■────■────────░─┤ RY(θ[3]) ├─░───■────■────────░─┤ RY(θ[6]) ├
├──────────┤ ░ ┌─┴─┐  │        ░ ├──────────┤ ░ ┌─┴─┐  │        ░ ├──────────┤
┤ RY(θ[1]) ├─░─┤ X ├──┼────■───░─┤ RY(θ[4]) ├─░─┤ X ├──┼────■───░─┤ RY(θ[7]) ├
├──────────┤ ░ └───┘┌─┴─┐┌─┴─┐ ░ ├──────────┤ ░ └───┘┌─┴─┐┌─┴─┐ ░ ├──────────┤
┤ RY(θ[2]) ├─░──────┤ X ├┤ X ├─░─┤ RY(θ[5]) ├─░──────┤ X ├┤ X ├─░─┤ RY(θ[8]) ├
└──────────┘ ░      └───┘└───┘ ░ └──────────┘ ░      └───┘└───┘ ░ └──────────┘

The entanglement can be set using the entanglement keyword as string or a list of index-pairs. See the documentation of TwoLocal and NLocal for more detail. 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 \(Y\) rotations can be reduced to a single \(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 ├───┤ 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]) ├────────────
     └──────────┘└───┘└──────────┘            └───┘└──────────┘

Create a new RealAmplitudes 2-local circuit.

Parameters
  • num_qubits (Optional[int]) – The number of qubits of the RealAmplitudes circuit.

  • reps (int) – Specifies how often the structure of a rotation layer followed by an entanglement layer is repeated.

  • entanglement (Union[str, List[List[int]], Callable[[int], List[int]]]) – 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 TwoLocal for more detail.

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

  • 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_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 (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 ParameterVector.

  • insert_barriers (bool) – If True, barriers are inserted in between each layer. If False, no barriers are inserted.

Attributes

RealAmplitudes.clbits

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

RealAmplitudes.data

Return the circuit data (instructions and context).

RealAmplitudes.entanglement

Get the entanglement strategy.

RealAmplitudes.entanglement_blocks

The blocks in the entanglement layers.

RealAmplitudes.extension_lib

RealAmplitudes.header

RealAmplitudes.initial_state

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

RealAmplitudes.insert_barriers

If barriers are inserted in between the layers or not.

RealAmplitudes.instances

RealAmplitudes.n_qubits

Deprecated, use num_qubits instead.

RealAmplitudes.num_clbits

Return number of classical bits.

RealAmplitudes.num_layers

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

RealAmplitudes.num_parameters

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

RealAmplitudes.num_parameters_settable

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

RealAmplitudes.num_qubits

Returns the number of qubits in this circuit.

RealAmplitudes.ordered_parameters

The parameters used in the underlying circuit.

RealAmplitudes.parameter_bounds

Return the parameter bounds.

RealAmplitudes.parameters

Get the Parameter objects in the circuit.

RealAmplitudes.preferred_init_points

The initial points for the parameters.

RealAmplitudes.prefix

RealAmplitudes.qregs

A list of the quantum registers associated with the circuit.

RealAmplitudes.qubits

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

RealAmplitudes.reps

The number of times rotation and entanglement block are repeated.

RealAmplitudes.rotation_blocks

The blocks in the rotation layers.

Methods

RealAmplitudes.AND(qr_variables, qb_target, …)

Build a collective conjunction (AND) circuit in place using mct.

RealAmplitudes.OR(qr_variables, qb_target, …)

Build a collective disjunction (OR) circuit in place using mct.

RealAmplitudes.__getitem__(item)

Return indexed operation.

RealAmplitudes.__len__()

Return number of operations in circuit.

RealAmplitudes.add_layer(other[, …])

Append another layer to the NLocal.

RealAmplitudes.add_register(*regs)

Add registers.

RealAmplitudes.append(instruction[, qargs, …])

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

RealAmplitudes.assign_parameters(param_dict)

Assign parameters to the n-local circuit.

RealAmplitudes.barrier(*qargs)

Apply Barrier.

RealAmplitudes.bind_parameters(value_dict)

Assign numeric parameters to values yielding a new circuit.

RealAmplitudes.cast(value, _type)

Best effort to cast value to type.

RealAmplitudes.cbit_argument_conversion(…)

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

RealAmplitudes.ccx(control_qubit1, …[, …])

Apply CCXGate.

RealAmplitudes.ch(control_qubit, target_qubit, *)

Apply CHGate.

RealAmplitudes.cls_instances()

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

RealAmplitudes.cls_prefix()

Return the prefix to use for auto naming.

RealAmplitudes.cnot(control_qubit, …[, …])

Apply CXGate.

RealAmplitudes.combine(rhs)

Append rhs to self if self contains compatible registers.

RealAmplitudes.compose(other[, qubits, …])

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

RealAmplitudes.copy([name])

Copy the circuit.

RealAmplitudes.count_ops()

Count each operation kind in the circuit.

RealAmplitudes.crx(theta, control_qubit, …)

Apply CRXGate.

RealAmplitudes.cry(theta, control_qubit, …)

Apply CRYGate.

RealAmplitudes.crz(theta, control_qubit, …)

Apply CRZGate.

RealAmplitudes.cswap(control_qubit, …[, …])

Apply CSwapGate.

RealAmplitudes.cu1(theta, control_qubit, …)

Apply CU1Gate.

RealAmplitudes.cu3(theta, phi, lam, …[, …])

Apply CU3Gate.

RealAmplitudes.cx(control_qubit, target_qubit, *)

Apply CXGate.

RealAmplitudes.cy(control_qubit, target_qubit, *)

Apply CYGate.

RealAmplitudes.cz(control_qubit, target_qubit, *)

Apply CZGate.

RealAmplitudes.dcx(qubit1, qubit2)

Apply DCXGate.

RealAmplitudes.decompose()

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

RealAmplitudes.depth()

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

RealAmplitudes.diag_gate(diag, qubit)

Deprecated version of QuantumCircuit.diagonal.

RealAmplitudes.diagonal(diag, qubit)

Attach a diagonal gate to a circuit.

RealAmplitudes.draw([output, scale, …])

Draw the quantum circuit.

RealAmplitudes.extend(rhs)

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

RealAmplitudes.fredkin(control_qubit, …[, …])

Apply CSwapGate.

RealAmplitudes.from_qasm_file(path)

Take in a QASM file and generate a QuantumCircuit object.

RealAmplitudes.from_qasm_str(qasm_str)

Take in a QASM string and generate a QuantumCircuit object.

RealAmplitudes.get_entangler_map(rep_num, …)

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

RealAmplitudes.get_unentangled_qubits()

Get the indices of unentangled qubits in a set.

RealAmplitudes.h(qubit, *[, q])

Apply HGate.

RealAmplitudes.hamiltonian(operator, time, …)

Apply hamiltonian evolution to to qubits.

RealAmplitudes.has_register(register)

Test if this circuit has the register r.

RealAmplitudes.i(qubit, *[, q])

Apply IGate.

RealAmplitudes.id(qubit, *[, q])

Apply IGate.

RealAmplitudes.iden(qubit, *[, q])

Deprecated identity gate.

RealAmplitudes.initialize(params, qubits)

Apply initialize to circuit.

RealAmplitudes.inverse()

Invert this circuit.

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

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

RealAmplitudes.isometry(isometry, q_input, …)

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

RealAmplitudes.iswap(qubit1, qubit2)

Apply iSwapGate.

RealAmplitudes.mcmt(gate, control_qubits, …)

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

RealAmplitudes.mcrx(theta, q_controls, q_target)

Apply Multiple-Controlled X rotation gate

RealAmplitudes.mcry(theta, q_controls, …)

Apply Multiple-Controlled Y rotation gate

RealAmplitudes.mcrz(lam, q_controls, q_target)

Apply Multiple-Controlled Z rotation gate

RealAmplitudes.mct(control_qubits, target_qubit)

Apply MCXGate.

RealAmplitudes.mcu1(lam, control_qubits, …)

Apply MCU1Gate.

RealAmplitudes.mcx(control_qubits, target_qubit)

Apply MCXGate.

RealAmplitudes.measure(qubit, cbit)

Measure quantum bit into classical bit (tuples).

RealAmplitudes.measure_active([inplace])

Adds measurement to all non-idle qubits.

RealAmplitudes.measure_all([inplace])

Adds measurement to all qubits.

RealAmplitudes.mirror()

Mirror the circuit by reversing the instructions.

RealAmplitudes.ms(theta, qubits)

Apply MSGate.

RealAmplitudes.num_connected_components([…])

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

RealAmplitudes.num_nonlocal_gates()

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

RealAmplitudes.num_tensor_factors()

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

RealAmplitudes.num_unitary_factors()

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

RealAmplitudes.print_settings()

Returns information about the setting.

RealAmplitudes.qasm([formatted, filename])

Return OpenQASM string.

RealAmplitudes.qbit_argument_conversion(…)

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

RealAmplitudes.r(theta, phi, qubit, *[, q])

Apply RGate.

RealAmplitudes.rcccx(control_qubit1, …)

Apply RC3XGate.

RealAmplitudes.rccx(control_qubit1, …)

Apply RCCXGate.

RealAmplitudes.remove_final_measurements([…])

Removes final measurement on all qubits if they are present.

RealAmplitudes.reset(qubit)

Reset q.

RealAmplitudes.rx(theta, qubit, *[, label, q])

Apply RXGate.

RealAmplitudes.rxx(theta, qubit1, qubit2)

Apply RXXGate.

RealAmplitudes.ry(theta, qubit, *[, label, q])

Apply RYGate.

RealAmplitudes.ryy(theta, qubit1, qubit2)

Apply RYYGate.

RealAmplitudes.rz(phi, qubit, *[, q])

Apply RZGate.

RealAmplitudes.rzx(theta, qubit1, qubit2)

Apply RZXGate.

RealAmplitudes.rzz(theta, qubit1, qubit2)

Apply RZZGate.

RealAmplitudes.s(qubit, *[, q])

Apply SGate.

RealAmplitudes.sdg(qubit, *[, q])

Apply SdgGate.

RealAmplitudes.size()

Returns total number of gate operations in circuit.

RealAmplitudes.snapshot(label[, …])

Take a statevector snapshot of the internal simulator representation.

RealAmplitudes.snapshot_density_matrix(label)

Take a density matrix snapshot of simulator state.

RealAmplitudes.snapshot_expectation_value(…)

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

RealAmplitudes.snapshot_probabilities(label, …)

Take a probability snapshot of the simulator state.

RealAmplitudes.snapshot_stabilizer(label)

Take a stabilizer snapshot of the simulator state.

RealAmplitudes.snapshot_statevector(label)

Take a statevector snapshot of the simulator state.

RealAmplitudes.squ(unitary_matrix, qubit[, …])

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

RealAmplitudes.swap(qubit1, qubit2)

Apply SwapGate.

RealAmplitudes.t(qubit, *[, q])

Apply TGate.

RealAmplitudes.tdg(qubit, *[, q])

Apply TdgGate.

RealAmplitudes.to_gate([parameter_map])

Create a Gate out of this circuit.

RealAmplitudes.to_instruction([parameter_map])

Create an Instruction out of this circuit.

RealAmplitudes.toffoli(control_qubit1, …)

Apply CCXGate.

RealAmplitudes.u1(theta, qubit, *[, q])

Apply U1Gate.

RealAmplitudes.u2(phi, lam, qubit, *[, q])

Apply U2Gate.

RealAmplitudes.u3(theta, phi, lam, qubit, *)

Apply U3Gate.

RealAmplitudes.uc(gate_list, q_controls, …)

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

RealAmplitudes.ucg(angle_list, q_controls, …)

Deprecated version of uc.

RealAmplitudes.ucrx(angle_list, q_controls, …)

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

RealAmplitudes.ucry(angle_list, q_controls, …)

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

RealAmplitudes.ucrz(angle_list, q_controls, …)

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

RealAmplitudes.ucx(angle_list, q_controls, …)

Deprecated version of ucrx.

RealAmplitudes.ucy(angle_list, q_controls, …)

Deprecated version of ucry.

RealAmplitudes.ucz(angle_list, q_controls, …)

Deprecated version of ucrz.

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

Apply unitary gate to q.

RealAmplitudes.width()

Return number of qubits plus clbits in circuit.

RealAmplitudes.x(qubit, *[, label, …])

Apply XGate.

RealAmplitudes.y(qubit, *[, q])

Apply YGate.

RealAmplitudes.z(qubit, *[, q])

Apply ZGate.

RealAmplitudes.__getitem__(item)

Return indexed operation.

RealAmplitudes.__len__()

Return number of operations in circuit.