Skip to main contentIBM Quantum Documentation
You are viewing the API reference for an old version of Qiskit SDK. Switch to latest version

WeightedAdder

WeightedAdder(num_state_qubits=None, weights=None, name='adder') GitHub(opens in a new tab)

A circuit to compute the weighted sum of qubit registers.

Given nn qubit basis states q0,,qn1{0,1}q_0, \ldots, q_{n-1} \in \{0, 1\} and non-negative integer weights λ0,,λn1\lambda_0, \ldots, \lambda_{n-1}, this circuit performs the operation

q0qn10sq0qn1j=0n1λjqjs|q_0 \ldots q_{n-1}\rangle |0\rangle_s \mapsto |q_0 \ldots q_{n-1}\rangle |\sum_{j=0}^{n-1} \lambda_j q_j\rangle_s

where ss is the number of sum qubits required. This can be computed as

s=1+log2(j=0n1λj)s = 1 + \left\lfloor \log_2\left( \sum_{j=0}^{n-1} \lambda_j \right) \right\rfloor

or s=1s = 1 if the sum of the weights is 0 (then the expression in the logarithm is invalid).

For qubits in a circuit diagram, the first weight applies to the upper-most qubit. For an example where the state of 4 qubits is added into a sum register, the circuit can be schematically drawn as

           ┌────────┐
  state_0:0| state_0 * weights[0]
           │        │ |
  state_1:1| + state_1 * weights[1]
           │        │ |
  state_2:2| + state_2 * weights[2]
           │        │ |
  state_3:3| + state_3 * weights[3]
           │        │
    sum_0:4|
           │  Adder │ |
    sum_1:5| = sum_0 * 2^0 + sum_1 * 2^1 + sum_2 * 2^2
           │        │ |
    sum_2:6|
           │        │
  carry_0:7
           │        │
  carry_1:8
           │        │
control_0:9
           └────────┘

Computes the weighted sum controlled by state qubits.

Parameters

  • num_state_qubits (Optional[int]) – The number of state qubits.
  • weights (Optional[List[int]]) – List of weights, one for each state qubit. If none are provided they default to 1 for every qubit.
  • name (str) – The name of the circuit.

Attributes

clbits

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

data

Return the circuit data (instructions and context).

Returns

a list-like object containing the tuples for the circuit’s data.

Each tuple is in the format (instruction, qargs, cargs), where instruction is an Instruction (or subclass) object, qargs is a list of Qubit objects, and cargs is a list of Clbit objects.

Return type

QuantumCircuitData

extension_lib

= 'include "qelib1.inc";'

= 'OPENQASM 2.0;'

instances

= 8

n_qubits

Deprecated, use num_qubits instead. Return number of qubits.

num_ancilla_qubits

int

The number of ancilla qubits required to implement the weighted sum.

Return type

int

Returns

The number of ancilla qubits in the circuit.

num_carry_qubits

int

The number of carry qubits required to compute the sum.

Note that this is not necessarily equal to the number of ancilla qubits, these can be queried using num_ancilla_qubits.

Return type

int

Returns

The number of carry qubits required to compute the sum.

num_clbits

Return number of classical bits.

num_control_qubits

int

The number of additional control qubits required.

Note that the total number of ancilla qubits can be obtained by calling the method num_ancilla_qubits.

Return type

int

Returns

The number of additional control qubits required (0 or 1).

num_parameters

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

num_qubits

Return number of qubits.

num_state_qubits

int

The number of qubits to be summed.

Return type

int

Returns

The number of state qubits.

num_sum_qubits

int

The number of sum qubits in the circuit.

Return type

int

Returns

The number of qubits needed to represent the weighted sum of the qubits.

parameters

Convenience function to get the parameters defined in the parameter table.

prefix

= 'circuit'

qregs

A list of the quantum registers associated with the circuit.

qubits

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

weights

List[int]

The weights for the qubit states.

Return type

List[int]

Returns

The weight for the qubit states.


Methods

AND

WeightedAdder.AND(qr_variables, qb_target, qr_ancillae, flags=None, mct_mode='no-ancilla')

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

Parameters

  • self (QuantumCircuit) – The QuantumCircuit object to build the conjunction on.
  • qr_variables (QuantumRegister) – The QuantumRegister holding the variable qubits.
  • qb_target (Qubit) – The target qubit to hold the conjunction result.
  • qr_ancillae (QuantumRegister) – The ancillary QuantumRegister for building the mct.
  • flags (list[int]) – A list of +1/-1/0 to mark negations or omissions of qubits.
  • mct_mode (str) – The mct building mode.

OR

WeightedAdder.OR(qr_variables, qb_target, qr_ancillae, flags=None, mct_mode='basic')

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

Parameters

  • self (QuantumCircuit) – The QuantumCircuit object to build the disjunction on.
  • qr_variables (QuantumRegister) – The QuantumRegister holding the variable qubits.
  • flags (list[int]) – A list of +1/-1/0 to mark negations or omissions of qubits.
  • qb_target (Qubit) – The target qubit to hold the disjunction result.
  • qr_ancillae (QuantumRegister) – The ancillary QuantumRegister for building the mct.
  • mct_mode (str) – The mct building mode.

__getitem__

WeightedAdder.__getitem__(item)

Return indexed operation.

__len__

WeightedAdder.__len__()

Return number of operations in circuit.

add_register

WeightedAdder.add_register(*regs)

Add registers.

append

WeightedAdder.append(instruction, qargs=None, cargs=None)

Append one or more instructions to the end of the circuit, modifying the circuit in place. Expands qargs and cargs.

Parameters

  • instruction (qiskit.circuit.Instruction) – Instruction instance to append
  • qargs (list(argument)) – qubits to attach instruction to
  • cargs (list(argument)) – clbits to attach instruction to

Returns

a handle to the instruction that was just added

Return type

qiskit.circuit.Instruction

assign_parameters

WeightedAdder.assign_parameters(param_dict, inplace=False)

Assign parameters to new parameters or values.

The keys of the parameter dictionary must be Parameter instances in the current circuit. The values of the dictionary can either be numeric values or new parameter objects. The values can be assigned to the current circuit object or to a copy of it.

Parameters

  • param_dict (dict) – A dictionary specifying the mapping from current_parameter to new_parameter, where new_parameter can be a new parameter object or a numeric value.
  • inplace (bool) – If False, a copy of the circuit with the bound parameters is returned. If True the circuit instance itself is modified.

Raises

CircuitError – If param_dict contains parameters not present in the circuit

Returns

A copy of the circuit with bound parameters, if

inplace is True, otherwise None.

Return type

optional(QuantumCircuit)

Examples

>>> from qiskit.circuit import QuantumCircuit, Parameter
>>> circuit = QuantumCircuit(2)
>>> params = [Parameter('A'), Parameter('B'), Parameter('C')]
>>> circuit.ry(params[0], 0)
>>> circuit.crx(params[1], 0, 1)
>>> circuit.draw()
        ┌───────┐
q_0: |0>Ry(A) ├────■────
        └───────┘┌───┴───┐
q_1: |0>─────────┤ Rx(B)
                 └───────┘
>>> circuit.assign_parameters({params[0]: params[2]}, inplace=True)
>>> circuit.draw()
        ┌───────┐
q_0: |0>Ry(C) ├────■────
        └───────┘┌───┴───┐
q_1: |0>─────────┤ Rx(B)
                 └───────┘
>>> bound_circuit = circuit.assign_parameters({params[1]: 1, params[2]: 2})
>>> bound_circuit.draw()
        ┌───────┐
q_0: |0>Ry(2) ├────■────
        └───────┘┌───┴───┐
q_1: |0>─────────┤ Rx(1)
                 └───────┘
>>> bound_circuit.parameters  # this one has no free parameters anymore
set()
>>> circuit.parameters  # the original one is still parameterized
{Parameter(A), Parameter(C)}

barrier

WeightedAdder.barrier(*qargs)

Apply Barrier. If qargs is None, applies to all.

bind_parameters

WeightedAdder.bind_parameters(value_dict)

Assign numeric parameters to values yielding a new circuit.

To assign new Parameter objects or bind the values in-place, without yielding a new circuit, use the assign_parameters method.

Parameters

value_dict (dict) – {parameter: value, …}

Raises

  • CircuitError – If value_dict contains parameters not present in the circuit
  • TypeError – If value_dict contains a ParameterExpression in the values.

Returns

copy of self with assignment substitution.

Return type

QuantumCircuit

cast

static WeightedAdder.cast(value, _type)

Best effort to cast value to type. Otherwise, returns the value.

cbit_argument_conversion

WeightedAdder.cbit_argument_conversion(clbit_representation)

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

Parameters

clbit_representation (Object) – representation to expand

Returns

Where each tuple is a classical bit.

Return type

List(tuple)

ccx

WeightedAdder.ccx(control_qubit1, control_qubit2, target_qubit, *, ctl1=None, ctl2=None, tgt=None)

Apply CCXGate.

ch

WeightedAdder.ch(control_qubit, target_qubit, *, label=None, ctrl_state=None, ctl=None, tgt=None)

Apply CHGate.

cls_instances

classmethod WeightedAdder.cls_instances()

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

cls_prefix

classmethod WeightedAdder.cls_prefix()

Return the prefix to use for auto naming.

cnot

WeightedAdder.cnot(control_qubit, target_qubit, *, label=None, ctrl_state=None, ctl=None, tgt=None)

Apply CXGate.

combine

WeightedAdder.combine(rhs)

Append rhs to self if self contains compatible registers.

Two circuits are compatible if they contain the same registers or if they contain different registers with unique names. The returned circuit will contain all unique registers between both circuits.

Return self + rhs as a new object.

Parameters

rhs (QuantumCircuit) – The quantum circuit to append to the right hand side.

Returns

Returns a new QuantumCircuit object

Return type

QuantumCircuit

Raises

QiskitError – if the rhs circuit is not compatible

compose

WeightedAdder.compose(other, qubits=None, clbits=None, front=False, inplace=False)

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

other can be narrower or of equal width to self.

Parameters

  • other (qiskit.circuit.Instruction orQuantumCircuit or BaseOperator) – (sub)circuit to compose onto self.
  • qubits (list[Qubit|int]) – qubits of self to compose onto.
  • clbits (list[Clbit|int]) – clbits of self to compose onto.
  • front (bool) – If True, front composition will be performed (not implemented yet).
  • inplace (bool) – If True, modify the object. Otherwise return composed circuit.

Returns

the composed circuit (returns None if inplace==True).

Return type

QuantumCircuit

Raises

  • CircuitError – if composing on the front.
  • QiskitError – if other is wider or there are duplicate edge mappings.

Examples

>>> lhs.compose(rhs, qubits=[3, 2], inplace=True)
            ┌───┐                   ┌─────┐                ┌───┐
lqr_1_0: ───┤ H ├───    rqr_0: ──■──┤ Tdg ├    lqr_1_0: ───┤ H ├───────────────
            ├───┤              ┌─┴─┐└─────┘                ├───┤
lqr_1_1: ───┤ X ├───    rqr_1: ┤ X ├───────    lqr_1_1: ───┤ X ├───────────────
         ┌──┴───┴──┐           └───┘                    ┌──┴───┴──┐┌───┐
lqr_1_2:U1(0.1)+                     =  lqr_1_2:U1(0.1) ├┤ X ├───────
         └─────────┘                                    └─────────┘└─┬─┘┌─────┐
lqr_2_0: ─────■─────                           lqr_2_0: ─────■───────■──┤ Tdg ├
            ┌─┴─┐                                          ┌─┴─┐        └─────┘
lqr_2_1: ───┤ X ├───                           lqr_2_1: ───┤ X ├───────────────
            └───┘                                          └───┘
lcr_0: 0 ═══════════                           lcr_0: 0 ═══════════════════════
 
lcr_1: 0 ═══════════                           lcr_1: 0 ═══════════════════════

copy

WeightedAdder.copy(name=None)

Copy the circuit.

Parameters

name (str) – name to be given to the copied circuit. If None, then the name stays the same

Returns

a deepcopy of the current circuit, with the specified name

Return type

QuantumCircuit

count_ops

WeightedAdder.count_ops()

Count each operation kind in the circuit.

Returns

a breakdown of how many operations of each kind, sorted by amount.

Return type

OrderedDict

crx

WeightedAdder.crx(theta, control_qubit, target_qubit, *, label=None, ctrl_state=None, ctl=None, tgt=None)

Apply CRXGate.

cry

WeightedAdder.cry(theta, control_qubit, target_qubit, *, label=None, ctrl_state=None, ctl=None, tgt=None)

Apply CRYGate.

crz

WeightedAdder.crz(theta, control_qubit, target_qubit, *, label=None, ctrl_state=None, ctl=None, tgt=None)

Apply CRZGate.

cswap

WeightedAdder.cswap(control_qubit, target_qubit1, target_qubit2, *, label=None, ctrl_state=None, ctl=None, tgt1=None, tgt2=None)

Apply CSwapGate.

cu1

WeightedAdder.cu1(theta, control_qubit, target_qubit, *, label=None, ctrl_state=None, ctl=None, tgt=None)

Apply CU1Gate.

cu3

WeightedAdder.cu3(theta, phi, lam, control_qubit, target_qubit, *, label=None, ctrl_state=None, ctl=None, tgt=None)

Apply CU3Gate.

cx

WeightedAdder.cx(control_qubit, target_qubit, *, label=None, ctrl_state=None, ctl=None, tgt=None)

Apply CXGate.

cy

WeightedAdder.cy(control_qubit, target_qubit, *, label=None, ctrl_state=None, ctl=None, tgt=None)

Apply CYGate.

cz

WeightedAdder.cz(control_qubit, target_qubit, *, label=None, ctrl_state=None, ctl=None, tgt=None)

Apply CZGate.

dcx

WeightedAdder.dcx(qubit1, qubit2)

Apply DCXGate.

decompose

WeightedAdder.decompose()

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

Returns

a circuit one level decomposed

Return type

QuantumCircuit

depth

WeightedAdder.depth()

Return circuit depth (i.e., length of critical path). This does not include compiler or simulator directives such as ‘barrier’ or ‘snapshot’.

Returns

Depth of circuit.

Return type

int

Notes

The circuit depth and the DAG depth need not be the same.

diag_gate

WeightedAdder.diag_gate(diag, qubit)

Deprecated version of QuantumCircuit.diagonal.

diagonal

WeightedAdder.diagonal(diag, qubit)

Attach a diagonal gate to a circuit.

The decomposition is based on Theorem 7 given in “Synthesis of Quantum Logic Circuits” by Shende et al. (https://arxiv.org/pdf/quant-ph/0406176.pdf(opens in a new tab)).

Parameters

  • diag (list) – list of the 2^k diagonal entries (for a diagonal gate on k qubits). Must contain at least two entries
  • qubit (QuantumRegister|list) – list of k qubits the diagonal is acting on (the order of the qubits specifies the computational basis in which the diagonal gate is provided: the first element in diag acts on the state where all the qubits in q are in the state 0, the second entry acts on the state where all the qubits q[1],…,q[k-1] are in the state zero and q[0] is in the state 1, and so on)

Returns

the diagonal gate which was attached to the circuit.

Return type

QuantumCircuit

Raises

QiskitError – if the list of the diagonal entries or the qubit list is in bad format; if the number of diagonal entries is not 2^k, where k denotes the number of qubits

draw

WeightedAdder.draw(output=None, scale=0.7, filename=None, style=None, interactive=False, line_length=None, plot_barriers=True, reverse_bits=False, justify=None, vertical_compression='medium', idle_wires=True, with_layout=True, fold=None, ax=None, initial_state=False, cregbundle=False)

Draw the quantum circuit.

text: ASCII art TextDrawing that can be printed in the console.

latex: high-quality images compiled via LaTeX.

latex_source: raw uncompiled LaTeX output.

matplotlib: images with color rendered purely in Python.

Parameters

  • output (str) – Select the output method to use for drawing the circuit. Valid choices are text, latex, latex_source, or mpl. By default the ‘text’ drawer is used unless a user config file has an alternative backend set as the default. If the output kwarg is set, that backend will always be used over the default in a user config file.
  • scale (float) – scale of image to draw (shrink if < 1)
  • filename (str) – file path to save image to
  • style (dict or str) – dictionary of style or file name of style file. This option is only used by the mpl output type. If a str is passed in that is the path to a json file which contains a dictionary of style, then that will be opened, parsed, and used as the input dict. See: Style Dict Doc for more information on the contents.
  • interactive (bool) – when set true show the circuit in a new window (for mpl this depends on the matplotlib backend being used supporting this). Note when used with either the text or the latex_source output type this has no effect and will be silently ignored.
  • line_length (int) – Deprecated; see fold which supersedes this option. Sets the length of the lines generated by text output type. This is useful when the drawing does not fit in the console. If None (default), it will try to guess the console width using shutil.get_terminal_size(). However, if you’re running in jupyter, the default line length is set to 80 characters. If you don’t want pagination at all, set line_length=-1.
  • reverse_bits (bool) – When set to True, reverse the bit order inside registers for the output visualization.
  • plot_barriers (bool) – Enable/disable drawing barriers in the output circuit. Defaults to True.
  • justify (string) – Options are left, right or none. If anything else is supplied it defaults to left justified. It refers to where gates should be placed in the output circuit if there is an option. none results in each gate being placed in its own column.
  • vertical_compression (string) – high, medium or low. It merges the lines generated by the text output so the drawing will take less vertical room. Default is medium. Only used by the text output, will be silently ignored otherwise.
  • idle_wires (bool) – Include idle wires (wires with no circuit elements) in output visualization. Default is True.
  • with_layout (bool) – Include layout information, with labels on the physical layout. Default is True.
  • fold (int) – Sets pagination. It can be disabled using -1. In text, sets the length of the lines. This is useful when the drawing does not fit in the console. If None (default), it will try to guess the console width using shutil. get_terminal_size(). However, if running in jupyter, the default line length is set to 80 characters. In mpl is the number of (visual) layers before folding. Default is 25.
  • ax (matplotlib.axes.Axes) – An optional Axes object to be used for the visualization output. If none is specified, a new matplotlib Figure will be created and used. Additionally, if specified, there will be no returned Figure since it is redundant. This is only used when the output kwarg is set to use the mpl backend. It will be silently ignored with all other outputs.
  • initial_state (bool) – Optional. Adds |0> in the beginning of the wire. Only used by the text, latex and latex_source outputs. Default: False.
  • cregbundle (bool) – Optional. If set True bundle classical registers. Only used by the text output. Default: False.

Returns

PIL.Image or matplotlib.figure or str or TextDrawing:

  • PIL.Image (output=’latex’)

    an in-memory representation of the image of the circuit diagram.

  • matplotlib.figure.Figure (output=’mpl’)

    a matplotlib figure object for the circuit diagram.

  • str (output=’latex_source’)

    The LaTeX source code for visualizing the circuit diagram.

  • TextDrawing (output=’text’)

    A drawing that can be printed as ASCII art.

Raises

  • VisualizationError – when an invalid output method is selected
  • ImportError – when the output methods require non-installed libraries

Style Dict Details

The style dict kwarg contains numerous options that define the style of the output circuit visualization. The style dict is only used by the mpl output. The options available in the style dict are defined below:

Parameters

  • textcolor (str) – The color code to use for text. Defaults to ‘#000000’

  • subtextcolor (str) – The color code to use for subtext. Defaults to ‘#000000’

  • linecolor (str) – The color code to use for lines. Defaults to ‘#000000’

  • creglinecolor (str) – The color code to use for classical register lines. Defaults to ‘#778899’

  • gatetextcolor (str) – The color code to use for gate text. Defaults to ‘#000000’

  • gatefacecolor (str) – The color code to use for gates. Defaults to ‘#ffffff’

  • barrierfacecolor (str) – The color code to use for barriers. Defaults to ‘#bdbdbd’

  • backgroundcolor (str) – The color code to use for the background. Defaults to ‘#ffffff’

  • fontsize (int) – The font size to use for text. Defaults to 13.

  • subfontsize (int) – The font size to use for subtext. Defaults to 8.

  • displaytext (dict) –

    A dictionary of the text to use for each element type in the output visualization. The default values are:

    {
        'id': 'id',
        'u0': 'U_0',
        'u1': 'U_1',
        'u2': 'U_2',
        'u3': 'U_3',
        'x': 'X',
        'y': 'Y',
        'z': 'Z',
        'h': 'H',
        's': 'S',
        'sdg': 'S^\dagger',
        't': 'T',
        'tdg': 'T^\dagger',
        'rx': 'R_x',
        'ry': 'R_y',
        'rz': 'R_z',
        'reset': '\left|0\right\rangle'
    }

    You must specify all the necessary values if using this. There is no provision for passing an incomplete dict in.

  • displaycolor (dict) –

    The color codes to use for each circuit

    element. The default values are:

    {
        'id': '#F0E442',
        'u0': '#E7AB3B',
        'u1': '#E7AB3B',
        'u2': '#E7AB3B',
        'u3': '#E7AB3B',
        'x': '#58C698',
        'y': '#58C698',
        'z': '#58C698',
        'h': '#70B7EB',
        's': '#E0722D',
        'sdg': '#E0722D',
        't': '#E0722D',
        'tdg': '#E0722D',
        'rx': '#ffffff',
        'ry': '#ffffff',
        'rz': '#ffffff',
        'reset': '#D188B4',
        'target': '#70B7EB',
        'meas': '#D188B4'
    }

    Also, just like displaytext there is no provision for an incomplete dict passed in.

  • latexdrawerstyle (bool) – When set to True, enable LaTeX mode, which will draw gates like the latex output modes.

  • usepiformat (bool) – When set to True, use radians for output.

  • fold (int) – The number of circuit elements to fold the circuit at. Defaults to 20.

  • cregbundle (bool) – If set True, bundle classical registers

  • showindex (bool) – If set True, draw an index.

  • compress (bool) – If set True, draw a compressed circuit.

  • figwidth (int) – The maximum width (in inches) for the output figure.

  • dpi (int) – The DPI to use for the output image. Defaults to 150.

  • margin (list) – A list of margin values to adjust spacing around output image. Takes a list of 4 ints: [x left, x right, y bottom, y top].

  • creglinestyle (str) – The style of line to use for classical registers. Choices are ‘solid’, ‘doublet’, or any valid matplotlib linestyle kwarg value. Defaults to doublet

extend

WeightedAdder.extend(rhs)

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

Two circuits are compatible if they contain the same registers or if they contain different registers with unique names. The returned circuit will contain all unique registers between both circuits.

Modify and return self.

Parameters

rhs (QuantumCircuit) – The quantum circuit to append to the right hand side.

Returns

Returns this QuantumCircuit object (which has been modified)

Return type

QuantumCircuit

Raises

QiskitError – if the rhs circuit is not compatible

fredkin

WeightedAdder.fredkin(control_qubit, target_qubit1, target_qubit2, *, ctl=None, tgt1=None, tgt2=None)

Apply CSwapGate.

from_qasm_file

static WeightedAdder.from_qasm_file(path)

Take in a QASM file and generate a QuantumCircuit object.

Parameters

path (str) – Path to the file for a QASM program

Returns

The QuantumCircuit object for the input QASM

Return type

QuantumCircuit

from_qasm_str

static WeightedAdder.from_qasm_str(qasm_str)

Take in a QASM string and generate a QuantumCircuit object.

Parameters

qasm_str (str) – A QASM program string

Returns

The QuantumCircuit object for the input QASM

Return type

QuantumCircuit

h

WeightedAdder.h(qubit, *, q=None)

Apply HGate.

hamiltonian

WeightedAdder.hamiltonian(operator, time, qubits, label=None)

Apply hamiltonian evolution to to qubits.

has_register

WeightedAdder.has_register(register)

Test if this circuit has the register r.

Parameters

register (Register) – a quantum or classical register.

Returns

True if the register is contained in this circuit.

Return type

bool

i

WeightedAdder.i(qubit, *, q=None)

Apply IGate.

id

WeightedAdder.id(qubit, *, q=None)

Apply IGate.

iden

WeightedAdder.iden(qubit, *, q=None)

Deprecated identity gate.

initialize

WeightedAdder.initialize(params, qubits)

Apply initialize to circuit.

inverse

WeightedAdder.inverse()

Invert this circuit.

This is done by recursively inverting all gates.

Returns

the inverted circuit

Return type

QuantumCircuit

Raises

CircuitError – if the circuit cannot be inverted.

iso

WeightedAdder.iso(isometry, q_input, q_ancillas_for_output, q_ancillas_zero=None, q_ancillas_dirty=None)

Attach an arbitrary isometry from m to n qubits to a circuit. In particular, this allows to attach arbitrary unitaries on n qubits (m=n) or to prepare any state on n qubits (m=0). The decomposition used here was introduced by Iten et al. in https://arxiv.org/abs/1501.06911(opens in a new tab).

Parameters

  • isometry (ndarray) – an isometry from m to n qubits, i.e., a (complex) ndarray of dimension 2^n×2^m with orthonormal columns (given in the computational basis specified by the order of the ancillas and the input qubits, where the ancillas are considered to be more significant than the input qubits.).
  • q_input (QuantumRegister|list[Qubit]) – list of m qubits where the input to the isometry is fed in (empty list for state preparation).
  • q_ancillas_for_output (QuantumRegister|list[Qubit]) – list of n-m ancilla qubits that are used for the output of the isometry and which are assumed to start in the zero state. The qubits are listed with increasing significance.
  • q_ancillas_zero (QuantumRegister|list[Qubit]) – list of ancilla qubits which are assumed to start in the zero state. Default is q_ancillas_zero = None.
  • q_ancillas_dirty (QuantumRegister|list[Qubit]) – list of ancilla qubits which can start in an arbitrary state. Default is q_ancillas_dirty = None.

Returns

the isometry is attached to the quantum circuit.

Return type

QuantumCircuit

Raises

QiskitError – if the array is not an isometry of the correct size corresponding to the provided number of qubits.

isometry

WeightedAdder.isometry(isometry, q_input, q_ancillas_for_output, q_ancillas_zero=None, q_ancillas_dirty=None)

Attach an arbitrary isometry from m to n qubits to a circuit. In particular, this allows to attach arbitrary unitaries on n qubits (m=n) or to prepare any state on n qubits (m=0). The decomposition used here was introduced by Iten et al. in https://arxiv.org/abs/1501.06911(opens in a new tab).

Parameters

  • isometry (ndarray) – an isometry from m to n qubits, i.e., a (complex) ndarray of dimension 2^n×2^m with orthonormal columns (given in the computational basis specified by the order of the ancillas and the input qubits, where the ancillas are considered to be more significant than the input qubits.).
  • q_input (QuantumRegister|list[Qubit]) – list of m qubits where the input to the isometry is fed in (empty list for state preparation).
  • q_ancillas_for_output (QuantumRegister|list[Qubit]) – list of n-m ancilla qubits that are used for the output of the isometry and which are assumed to start in the zero state. The qubits are listed with increasing significance.
  • q_ancillas_zero (QuantumRegister|list[Qubit]) – list of ancilla qubits which are assumed to start in the zero state. Default is q_ancillas_zero = None.
  • q_ancillas_dirty (QuantumRegister|list[Qubit]) – list of ancilla qubits which can start in an arbitrary state. Default is q_ancillas_dirty = None.

Returns

the isometry is attached to the quantum circuit.

Return type

QuantumCircuit

Raises

QiskitError – if the array is not an isometry of the correct size corresponding to the provided number of qubits.

iswap

WeightedAdder.iswap(qubit1, qubit2)

Apply iSwapGate.

mcmt

WeightedAdder.mcmt(gate, control_qubits, target_qubits, ancilla_qubits=None, mode='no-ancilla', *, single_control_gate_fun=None, q_controls=None, q_ancillae=None, q_targets=None)

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

This can also be used to implement a generic multi-control gate, as the target could also be of length 1.

mcrx

WeightedAdder.mcrx(theta, q_controls, q_target, use_basis_gates=False)

Apply Multiple-Controlled X rotation gate

Parameters

  • self (QuantumCircuit) – The QuantumCircuit object to apply the mcrx gate on.
  • theta (float) – angle theta
  • q_controls (list(Qubit)) – The list of control qubits
  • q_target (Qubit) – The target qubit
  • use_basis_gates (bool) – use u1, u2, u3, cx, id

Raises

QiskitError – parameter errors

mcry

WeightedAdder.mcry(theta, q_controls, q_target, q_ancillae, mode='basic', use_basis_gates=False)

Apply Multiple-Controlled Y rotation gate

Parameters

  • self (QuantumCircuit) – The QuantumCircuit object to apply the mcry gate on.
  • theta (float) – angle theta
  • q_controls (list(Qubit)) – The list of control qubits
  • q_target (Qubit) – The target qubit
  • q_ancillae (QuantumRegister or tuple(QuantumRegister, int)) – The list of ancillary qubits.
  • mode (string) – The implementation mode to use
  • use_basis_gates (bool) – use u1, u2, u3, cx, id

Raises

QiskitError – parameter errors

mcrz

WeightedAdder.mcrz(lam, q_controls, q_target, use_basis_gates=False)

Apply Multiple-Controlled Z rotation gate

Parameters

  • self (QuantumCircuit) – The QuantumCircuit object to apply the mcrz gate on.
  • lam (float) – angle lambda
  • q_controls (list(Qubit)) – The list of control qubits
  • q_target (Qubit) – The target qubit
  • use_basis_gates (bool) – use u1, u2, u3, cx, id

Raises

QiskitError – parameter errors

mct

WeightedAdder.mct(control_qubits, target_qubit, ancilla_qubits=None, mode='noancilla')

Apply MCXGate.

mcu1

WeightedAdder.mcu1(lam, control_qubits, target_qubit)

Apply MCU1Gate.

mcx

WeightedAdder.mcx(control_qubits, target_qubit, ancilla_qubits=None, mode='noancilla')

Apply MCXGate.

The multi-cX gate can be implemented using different techniques, which use different numbers of ancilla qubits and have varying circuit depth. These modes are: - ‘no-ancilla’: Requires 0 ancilla qubits. - ‘recursion’: Requires 1 ancilla qubit if more than 4 controls are used, otherwise 0. - ‘v-chain’: Requires 2 less ancillas than the number of control qubits. - ‘v-chain-dirty’: Same as for the clean ancillas (but the circuit will be longer).

measure

WeightedAdder.measure(qubit, cbit)

Measure quantum bit into classical bit (tuples).

Parameters

Returns

the attached measure instruction.

Return type

qiskit.Instruction

Raises

CircuitError – if qubit is not in this circuit or bad format; if cbit is not in this circuit or not creg.

measure_active

WeightedAdder.measure_active(inplace=True)

Adds measurement to all non-idle qubits. Creates a new ClassicalRegister with a size equal to the number of non-idle qubits being measured.

Returns a new circuit with measurements if inplace=False.

Parameters

inplace (bool) – All measurements inplace or return new circuit.

Returns

Returns circuit with measurements when inplace = False.

Return type

QuantumCircuit

measure_all

WeightedAdder.measure_all(inplace=True)

Adds measurement to all qubits. Creates a new ClassicalRegister with a size equal to the number of qubits being measured.

Returns a new circuit with measurements if inplace=False.

Parameters

inplace (bool) – All measurements inplace or return new circuit.

Returns

Returns circuit with measurements when inplace = False.

Return type

QuantumCircuit

mirror

WeightedAdder.mirror()

Mirror the circuit by reversing the instructions.

This is done by recursively mirroring all instructions. It does not invert any gate.

Returns

the mirrored circuit

Return type

QuantumCircuit

ms

WeightedAdder.ms(theta, qubits)

Apply MSGate.

num_connected_components

WeightedAdder.num_connected_components(unitary_only=False)

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

Parameters

unitary_only (bool) – Compute only unitary part of graph.

Returns

Number of connected components in circuit.

Return type

int

num_nonlocal_gates

WeightedAdder.num_nonlocal_gates()

Return number of non-local gates (i.e. involving 2+ qubits).

Conditional nonlocal gates are also included.

num_tensor_factors

WeightedAdder.num_tensor_factors()

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

Notes

This is here for backwards compatibility, and will be removed in a future release of Qiskit. You should call num_unitary_factors instead.

num_unitary_factors

WeightedAdder.num_unitary_factors()

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

qasm

WeightedAdder.qasm(formatted=False, filename=None)

Return OpenQASM string.

Parameters

  • formatted (bool) – Return formatted Qasm string.
  • filename (str) – Save Qasm to file with name ‘filename’.

Returns

If formatted=False.

Return type

str

Raises

ImportError – If pygments is not installed and formatted is True.

qbit_argument_conversion

WeightedAdder.qbit_argument_conversion(qubit_representation)

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

Parameters

qubit_representation (Object) – representation to expand

Returns

Where each tuple is a qubit.

Return type

List(tuple)

r

WeightedAdder.r(theta, phi, qubit, *, q=None)

Apply RGate.

rcccx

WeightedAdder.rcccx(control_qubit1, control_qubit2, control_qubit3, target_qubit)

Apply RC3XGate.

rccx

WeightedAdder.rccx(control_qubit1, control_qubit2, target_qubit)

Apply RCCXGate.

remove_final_measurements

WeightedAdder.remove_final_measurements(inplace=True)

Removes final measurement on all qubits if they are present. Deletes the ClassicalRegister that was used to store the values from these measurements if it is idle.

Returns a new circuit without measurements if inplace=False.

Parameters

inplace (bool) – All measurements removed inplace or return new circuit.

Returns

Returns circuit with measurements removed when inplace = False.

Return type

QuantumCircuit

reset

WeightedAdder.reset(qubit)

Reset q.

rx

WeightedAdder.rx(theta, qubit, *, label=None, q=None)

Apply RXGate.

rxx

WeightedAdder.rxx(theta, qubit1, qubit2)

Apply RXXGate.

ry

WeightedAdder.ry(theta, qubit, *, label=None, q=None)

Apply RYGate.

ryy

WeightedAdder.ryy(theta, qubit1, qubit2)

Apply RYYGate.

rz

WeightedAdder.rz(phi, qubit, *, q=None)

Apply RZGate.

rzx

WeightedAdder.rzx(theta, qubit1, qubit2)

Apply RZXGate.

rzz

WeightedAdder.rzz(theta, qubit1, qubit2)

Apply RZZGate.

s

WeightedAdder.s(qubit, *, q=None)

Apply SGate.

sdg

WeightedAdder.sdg(qubit, *, q=None)

Apply SdgGate.

size

WeightedAdder.size()

Returns total number of gate operations in circuit.

Returns

Total number of gate operations.

Return type

int

snapshot

WeightedAdder.snapshot(label, snapshot_type='statevector', qubits=None, params=None)

Take a statevector snapshot of the internal simulator representation. Works on all qubits, and prevents reordering (like barrier). :param label: a snapshot label to report the result :type label: str :param snapshot_type: the type of the snapshot. :type snapshot_type: str :param qubits: the qubits to apply snapshot to [Default: None]. :type qubits: list or None :param params: the parameters for snapshot_type [Default: None]. :type params: list or None

Returns

with attached command

Return type

QuantumCircuit

Raises

ExtensionError – malformed command

snapshot_density_matrix

WeightedAdder.snapshot_density_matrix(label, qubits=None)

Take a density matrix snapshot of simulator state.

Parameters

  • label (str) – a snapshot label to report the result
  • qubits (list or None) – the qubits to apply snapshot to. If None all qubits will be snapshot [Default: None].

Returns

with attached instruction.

Return type

QuantumCircuit

Raises

ExtensionError – if snapshot is invalid.

snapshot_expectation_value

WeightedAdder.snapshot_expectation_value(label, op, qubits, single_shot=False, variance=False)

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

Parameters

  • label (str) – a snapshot label to report the result
  • op (Operator) – operator to snapshot
  • qubits (list) – the qubits to snapshot.
  • single_shot (bool) – return list for each shot rather than average [Default: False]
  • variance (bool) – compute variance of probabilities [Default: False]

Returns

with attached instruction.

Return type

QuantumCircuit

Raises

ExtensionError – if snapshot is invalid.

snapshot_probabilities

WeightedAdder.snapshot_probabilities(label, qubits, variance=False)

Take a probability snapshot of the simulator state.

Parameters

  • label (str) – a snapshot label to report the result
  • qubits (list) – the qubits to snapshot.
  • variance (bool) – compute variance of probabilities [Default: False]

Returns

with attached instruction.

Return type

QuantumCircuit

Raises

ExtensionError – if snapshot is invalid.

snapshot_stabilizer

WeightedAdder.snapshot_stabilizer(label)

Take a stabilizer snapshot of the simulator state.

Parameters

label (str) – a snapshot label to report the result.

Returns

with attached instruction.

Return type

QuantumCircuit

Raises

ExtensionError – if snapshot is invalid.

Additional Information:

This snapshot is always performed on all qubits in a circuit. The number of qubits parameter specifies the size of the instruction as a barrier and should be set to the number of qubits in the circuit.

snapshot_statevector

WeightedAdder.snapshot_statevector(label)

Take a statevector snapshot of the simulator state.

Parameters

label (str) – a snapshot label to report the result.

Returns

with attached instruction.

Return type

QuantumCircuit

Raises

ExtensionError – if snapshot is invalid.

Additional Information:

This snapshot is always performed on all qubits in a circuit. The number of qubits parameter specifies the size of the instruction as a barrier and should be set to the number of qubits in the circuit.

squ

WeightedAdder.squ(unitary_matrix, qubit, mode='ZYZ', up_to_diagonal=False, *, u=None)

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

Note that the decomposition is up to a global phase shift. (This is a well known decomposition, which can be found for example in Nielsen and Chuang’s book “Quantum computation and quantum information”.)

Parameters

  • unitary_matrix (ndarray) – 2*2 unitary (given as a (complex) ndarray).
  • qubit (QuantumRegister |Qubit) – The qubit which the gate is acting on.
  • mode (string) – determines the used decomposition by providing the rotation axes. The allowed modes are: “ZYZ” (default)
  • up_to_diagonal (bool) – if set to True, the single-qubit unitary is decomposed up to a diagonal matrix, i.e. a unitary u’ is implemented such that there exists a 2*2 diagonal gate d with u = d.dot(u’)
  • u (ndarray) – Deprecated, use unitary_matrix instead.

Returns

The single-qubit unitary instruction attached to the circuit.

Return type

InstructionSet

Raises

QiskitError – if the format is wrong; if the array u is not unitary

swap

WeightedAdder.swap(qubit1, qubit2)

Apply SwapGate.

t

WeightedAdder.t(qubit, *, q=None)

Apply TGate.

tdg

WeightedAdder.tdg(qubit, *, q=None)

Apply TdgGate.

to_gate

WeightedAdder.to_gate(parameter_map=None)

Create a Gate out of this circuit.

Parameters

parameter_map (dict) – For parameterized circuits, a mapping from parameters in the circuit to parameters to be used in the gate. If None, existing circuit parameters will also parameterize the gate.

Returns

a composite gate encapsulating this circuit (can be decomposed back)

Return type

Gate

to_instruction

WeightedAdder.to_instruction(parameter_map=None)

Create an Instruction out of this circuit.

Parameters

parameter_map (dict) – For parameterized circuits, a mapping from parameters in the circuit to parameters to be used in the instruction. If None, existing circuit parameters will also parameterize the instruction.

Returns

a composite instruction encapsulating this circuit (can be decomposed back)

Return type

qiskit.circuit.Instruction

toffoli

WeightedAdder.toffoli(control_qubit1, control_qubit2, target_qubit, *, ctl1=None, ctl2=None, tgt=None)

Apply CCXGate.

u1

WeightedAdder.u1(theta, qubit, *, q=None)

Apply U1Gate.

u2

WeightedAdder.u2(phi, lam, qubit, *, q=None)

Apply U2Gate.

u3

WeightedAdder.u3(theta, phi, lam, qubit, *, q=None)

Apply U3Gate.

uc

WeightedAdder.uc(gate_list, q_controls, q_target, up_to_diagonal=False)

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

The decomposition was introduced by Bergholm et al. in https://arxiv.org/pdf/quant-ph/0410066.pdf(opens in a new tab).

Parameters

  • gate_list (list[ndarray]) – list of two qubit unitaries [U_0,…,U_{2^k-1}], where each single-qubit unitary U_i is a given as a 2*2 array
  • q_controls (QuantumRegister|list[(QuantumRegister,int)]) – list of k control qubits. The qubits are ordered according to their significance in the computational basis. For example if q_controls=[q[1],q[2]] (with q = QuantumRegister(2)), the unitary U_0 is performed if q[1] and q[2] are in the state zero, U_1 is performed if q[2] is in the state zero and q[1] is in the state one, and so on
  • q_target (QuantumRegister|(QuantumRegister,int)) – target qubit, where we act on with the single-qubit gates.
  • up_to_diagonal (bool) – If set to True, the uniformly controlled gate is decomposed up to a diagonal gate, i.e. a unitary u’ is implemented such that there exists a diagonal gate d with u = d.dot(u’), where the unitary u describes the uniformly controlled gate

Returns

the uniformly controlled gate is attached to the circuit.

Return type

QuantumCircuit

Raises

QiskitError – if the list number of control qubits does not correspond to the provided number of single-qubit unitaries; if an input is of the wrong type

ucg

WeightedAdder.ucg(angle_list, q_controls, q_target, up_to_diagonal=False)

Deprecated version of uc.

ucrx

WeightedAdder.ucrx(angle_list, q_controls, q_target)

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

The decomposition is base on https://arxiv.org/pdf/quant-ph/0406176.pdf(opens in a new tab) by Shende et al.

Parameters

  • angle_list (list) – list of (real) rotation angles [a0,...,a2k1][a_0,...,a_{2^k-1}]
  • q_controls (QuantumRegister|list) – list of k control qubits (or empty list if no controls). The control qubits are ordered according to their significance in increasing order: For example if q_controls=[q[0],q[1]] (with q = QuantumRegister(2)), the rotation Rx(a_0) is performed if q[0] and q[1] are in the state zero, the rotation Rx(a_1) is performed if q[0] is in the state one and q[1] is in the state zero, and so on
  • q_target (QuantumRegister|Qubit) – target qubit, where we act on with the single-qubit rotation gates

Returns

the uniformly controlled rotation gate is attached to the circuit.

Return type

QuantumCircuit

Raises

QiskitError – if the list number of control qubits does not correspond to the provided number of single-qubit unitaries; if an input is of the wrong type

ucry

WeightedAdder.ucry(angle_list, q_controls, q_target)

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

The decomposition is base on https://arxiv.org/pdf/quant-ph/0406176.pdf(opens in a new tab) by Shende et al.

Parameters

  • angle_list (list[numbers) – list of (real) rotation angles [a0,...,a2k1][a_0,...,a_{2^k-1}]
  • q_controls (QuantumRegister|list[Qubit]) – list of k control qubits (or empty list if no controls). The control qubits are ordered according to their significance in increasing order: For example if q_controls=[q[0],q[1]] (with q = QuantumRegister(2)), the rotation Ry(a_0) is performed if q[0] and q[1] are in the state zero, the rotation Ry(a_1) is performed if q[0] is in the state one and q[1] is in the state zero, and so on
  • q_target (QuantumRegister|Qubit) – target qubit, where we act on with the single-qubit rotation gates

Returns

the uniformly controlled rotation gate is attached to the circuit.

Return type

QuantumCircuit

Raises

QiskitError – if the list number of control qubits does not correspond to the provided number of single-qubit unitaries; if an input is of the wrong type

ucrz

WeightedAdder.ucrz(angle_list, q_controls, q_target)

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

The decomposition is base on https://arxiv.org/pdf/quant-ph/0406176.pdf(opens in a new tab) by Shende et al.

Parameters

  • angle_list (list[numbers) – list of (real) rotation angles [a_0,…,a_{2^k-1}]
  • q_controls (QuantumRegister|list[Qubit]) – list of k control qubits (or empty list if no controls). The control qubits are ordered according to their significance in increasing order: For example if q_controls=[q[1],q[2]] (with q = QuantumRegister(2)), the rotation Rz(a_0)is performed if q[1] and q[2] are in the state zero, the rotation Rz(a_1) is performed if q[1] is in the state one and q[2] is in the state zero, and so on
  • q_target (QuantumRegister|Qubit) – target qubit, where we act on with the single-qubit rotation gates

Returns

the uniformly controlled rotation gate is attached to the circuit.

Return type

QuantumCircuit

Raises

QiskitError – if the list number of control qubits does not correspond to the provided number of single-qubit unitaries; if an input is of the wrong type

ucx

WeightedAdder.ucx(angle_list, q_controls, q_target)

Deprecated version of ucrx.

ucy

WeightedAdder.ucy(angle_list, q_controls, q_target)

Deprecated version of ucry.

ucz

WeightedAdder.ucz(angle_list, q_controls, q_target)

Deprecated version of ucrz.

unitary

WeightedAdder.unitary(obj, qubits, label=None)

Apply unitary gate to q.

width

WeightedAdder.width()

Return number of qubits plus clbits in circuit.

Returns

Width of circuit.

Return type

int

x

WeightedAdder.x(qubit, *, label=None, ctrl_state=None, q=None)

Apply XGate.

y

WeightedAdder.y(qubit, *, q=None)

Apply YGate.

z

WeightedAdder.z(qubit, *, q=None)

Apply ZGate.

Was this page helpful?