Operator

class qiskit.quantum_info.Operator(data, input_dims=None, output_dims=None)

GitHub

Bases: LinearOp

Matrix operator class

This represents a matrix operator $M$ that will evolve() a Statevector $|\psi\rangle$ by matrix-vector multiplication

$|\psi\rangle \mapsto M|\psi\rangle,$

and will evolve() a DensityMatrix $\rho$ by left and right multiplication

$\rho \mapsto M \rho M^\dagger.$

For example, the following operator $M = X$ applied to the zero state $|\psi\rangle=|0\rangle (\rho = |0\rangle\langle 0|)$ changes it to the one state $|\psi\rangle=|1\rangle (\rho = |1\rangle\langle 1|)$:

>>> import numpy as np
>>> from qiskit.quantum_info import Operator
>>> op = Operator(np.array([[0.0, 1.0], [1.0, 0.0]]))  # Represents Pauli X operator

>>> from qiskit.quantum_info import Statevector
>>> sv = Statevector(np.array([1.0, 0.0]))
>>> sv.evolve(op)
Statevector([0.+0.j, 1.+0.j],
dims=(2,))

>>> from qiskit.quantum_info import DensityMatrix
>>> dm = DensityMatrix(np.array([[1.0, 0.0], [0.0, 0.0]]))
>>> dm.evolve(op)
DensityMatrix([[0.+0.j, 0.+0.j],
[0.+0.j, 1.+0.j]],
dims=(2,))

Initialize an operator object.

Parameters

• data (QuantumCircuit orOperation or BaseOperator or matrix) – data to initialize operator.
• input_dims (tuple) – the input subsystem dimensions. [Default: None]
• output_dims (tuple) – the output subsystem dimensions. [Default: None]

Raises

QiskitError – if input data cannot be initialized as an operator.

If the input or output dimensions are None, they will be automatically determined from the input data. If the input data is a Numpy array of shape (2**N, 2**N) qubit systems will be used. If the input operator is not an N-qubit operator, it will assign a single subsystem with dimension specified by the shape of the input. Note that two operators initialized via this method are only considered equivalent if they match up to their canonical qubit order (or: permutation). See Operator.from_circuit() to specify a different qubit permutation.

Attributes

atol

Default value: 1e-08

data

The underlying Numpy array.

dim

Return tuple (input_shape, output_shape).

num_qubits

Return the number of qubits if a N-qubit operator or None otherwise.

qargs

Return the qargs for the operator.

rtol

Default value: 1e-05

settings

Return operator settings.

Methods

adjoint()

GitHub

Return the adjoint of the Operator.

Return type

Self

apply_permutation

apply_permutation(perm, front=False)

GitHub

Modifies operator’s data by composing it with a permutation.

Parameters

• perm (list) – permutation pattern, describing which qubits occupy the positions 0, 1, 2, etc. after applying the permutation.
• front (bool) – When set to True the permutation is applied before the operator, when set to False the permutation is applied after the operator.

Returns

The modified operator.

Return type

Operator

Raises

QiskitError – if the size of the permutation pattern does not match the dimensions of the operator.

compose

compose(other, qargs=None, front=False)

GitHub

Return the operator composition with another Operator.

Parameters

• other (Operator) – a Operator object.
• qargs (list or None) – Optional, a list of subsystem positions to apply other on. If None apply on all subsystems (default: None).
• front (bool) – If True compose using right operator multiplication, instead of left multiplication [default: False].

Returns

The composed Operator.

Return type

Operator

Raises

QiskitError – if other cannot be converted to an operator, or has incompatible dimensions for specified subsystems.

Note

Composition (&) by default is defined as left matrix multiplication for matrix operators, while @ (equivalent to dot()) is defined as right matrix multiplication. That is that A & B == A.compose(B) is equivalent to B @ A == B.dot(A) when A and B are of the same type.

Setting the front=True kwarg changes this to right matrix multiplication and is equivalent to the dot() method A.dot(B) == A.compose(B, front=True).

conjugate

conjugate()

GitHub

Return the conjugate of the Operator.

copy

copy()

GitHub

Make a deep copy of current operator.

dot

dot(other, qargs=None)

GitHub

Return the right multiplied operator self * other.

Parameters

• other (Operator) – an operator object.
• qargs (list or None) – Optional, a list of subsystem positions to apply other on. If None apply on all subsystems (default: None).

Returns

The right matrix multiplied Operator.

Return type

Operator

Note

The dot product can be obtained using the @ binary operator. Hence a.dot(b) is equivalent to a @ b.

draw

draw(output=None, **drawer_args)

GitHub

Return a visualization of the Operator.

repr: String of the state’s __repr__.

text: ASCII TextMatrix that can be printed in the console.

latex: An IPython Latex object for displaying in Jupyter Notebooks.

latex_source: Raw, uncompiled ASCII source to generate array using LaTeX.

Parameters

• output (str) – Select the output method to use for drawing the state. Valid choices are repr, text, latex, latex_source, Default is repr.
• drawer_args – Arguments to be passed directly to the relevant drawing function or constructor (TextMatrix(), array_to_latex()). See the relevant function under qiskit.visualization for that function’s documentation.

Returns

Drawing of the Operator.

Return type

str or TextMatrix or IPython.display.Latex

Raises

ValueError – when an invalid output method is selected.

equiv

equiv(other, rtol=None, atol=None)

GitHub

Return True if operators are equivalent up to global phase.

Parameters

• other (Operator) – an operator object.
• rtol (float) – relative tolerance value for comparison.
• atol (float) – absolute tolerance value for comparison.

Returns

True if operators are equivalent up to global phase.

Return type

bool

expand

expand(other)

GitHub

Return the reverse-order tensor product with another Operator.

Parameters

other (Operator) – a Operator object.

Returns

the tensor product $b \otimes a$, where $a$

is the current Operator, and $b$ is the other Operator.

Return type

Operator

from_circuit

classmethod from_circuit(circuit, ignore_set_layout=False, layout=None, final_layout=None)

GitHub

Create a new Operator object from a QuantumCircuit

While a QuantumCircuit object can passed directly as data to the class constructor this provides no options on how the circuit is used to create an Operator. This constructor method lets you control how the Operator is created so it can be adjusted for a particular use case.

By default this constructor method will permute the qubits based on a configured initial layout (i.e. after it was transpiled). It also provides an option to manually provide a Layout object directly.

Parameters

• circuit (QuantumCircuit) – The QuantumCircuit to create an Operator object from.
• ignore_set_layout (bool) – When set to True if the input circuit has a layout set it will be ignored
• layout (Layout) – If specified this kwarg can be used to specify a particular layout to use to permute the qubits in the created Operator. If this is specified it will be used instead of a layout contained in the circuit input. If specified the virtual bits in the Layout must be present in the circuit input.
• final_layout (Layout) – If specified this kwarg can be used to represent the output permutation caused by swap insertions during the routing stage of the transpiler.

Returns

An operator representing the input circuit

Return type

Operator

from_label

classmethod from_label(label)

GitHub

Return a tensor product of single-qubit operators.

Parameters

label (string) – single-qubit operator string.

Returns

The N-qubit operator.

Return type

Operator

Raises

QiskitError – if the label contains invalid characters, or the length of the label is larger than an explicitly specified num_qubits.

The labels correspond to the single-qubit matrices: ‘I’: [[1, 0], [0, 1]] ‘X’: [[0, 1], [1, 0]] ‘Y’: [[0, -1j], [1j, 0]] ‘Z’: [[1, 0], [0, -1]] ‘H’: [[1, 1], [1, -1]] / sqrt(2) ‘S’: [[1, 0], [0 , 1j]] ‘T’: [[1, 0], [0, (1+1j) / sqrt(2)]] ‘0’: [[1, 0], [0, 0]] ‘1’: [[0, 0], [0, 1]] ‘+’: [[0.5, 0.5], [0.5 , 0.5]] ‘-’: [[0.5, -0.5], [-0.5 , 0.5]] ‘r’: [[0.5, -0.5j], [0.5j , 0.5]] ‘l’: [[0.5, 0.5j], [-0.5j , 0.5]]

input_dims

input_dims(qargs=None)

GitHub

Return tuple of input dimension for specified subsystems.

is_unitary

is_unitary(atol=None, rtol=None)

GitHub

Return True if operator is a unitary matrix.

output_dims

output_dims(qargs=None)

GitHub

Return tuple of output dimension for specified subsystems.

power

power(n)

GitHub

Return the matrix power of the operator.

Parameters

n (float) – the power to raise the matrix to.

Returns

the resulting operator O ** n.

Return type

Operator

Raises

QiskitError – if the input and output dimensions of the operator are not equal.

reshape

reshape(input_dims=None, output_dims=None, num_qubits=None)

GitHub

Return a shallow copy with reshaped input and output subsystem dimensions.

Parameters

• input_dims (None or tuple) – new subsystem input dimensions. If None the original input dims will be preserved [Default: None].
• output_dims (None or tuple) – new subsystem output dimensions. If None the original output dims will be preserved [Default: None].
• num_qubits (None or int) – reshape to an N-qubit operator [Default: None].

Returns

returns self with reshaped input and output dimensions.

Return type

BaseOperator

Raises

QiskitError – if combined size of all subsystem input dimension or subsystem output dimensions is not constant.

reverse_qargs

reverse_qargs()

GitHub

Return an Operator with reversed subsystem ordering.

For a tensor product operator this is equivalent to reversing the order of tensor product subsystems. For an operator $A = A_{n-1} \otimes ... \otimes A_0$ the returned operator will be $A_0 \otimes ... \otimes A_{n-1}$.

Returns

the operator with reversed subsystem order.

Return type

Operator

tensor

tensor(other)

GitHub

Return the tensor product with another Operator.

Parameters

other (Operator) – a Operator object.

Returns

the tensor product $a \otimes b$, where $a$

is the current Operator, and $b$ is the other Operator.

Return type

Operator

Note

The tensor product can be obtained using the ^ binary operator. Hence a.tensor(b) is equivalent to a ^ b.

to_instruction

to_instruction()

GitHub

Convert to a UnitaryGate instruction.

to_matrix

to_matrix()

GitHub

Convert operator to NumPy matrix.

to_operator

to_operator()

GitHub

Convert operator to matrix operator class

Return type

Operator

transpose

transpose()

GitHub

Return the transpose of the Operator.