# qml.operation.Tensor¶

class Tensor(*args)[source]

Container class representing tensor products of observables.

To create a tensor, simply initiate it like so:

>>> T = Tensor(qml.PauliX(0), qml.Hermitian(A, [1, 2]))


You can also create a tensor from other Tensors:

>>> T = Tensor(T, qml.PauliZ(4))


The @ symbol can be used as a tensor product operation:

>>> T = qml.PauliX(0) @ qml.Hadamard(2)

 data Raw parameters of all constituent observables in the tensor product. eigvals Return the eigenvalues of the specified tensor product observable. hash returns an integer hash uniquely representing the operator id String for the ID of the operator. matrix Matrix representation of the tensor operator in the computational basis. name All constituent observable names making up the tensor product. non_identity_obs Returns the non-identity observables contained in the tensor product. num_params Raw parameters of all constituent observables in the tensor product. num_wires Number of wires the tensor product acts on. parameters Evaluated parameter values of all constituent observables in the tensor product. return_type tensor wires All wires in the system the tensor product acts on.
data

parameters of the operator

Type

list[Any]

eigvals

Return the eigenvalues of the specified tensor product observable.

This method uses pre-stored eigenvalues for standard observables where possible.

Returns

array containing the eigenvalues of the tensor product observable

Return type

array[float]

hash

returns an integer hash uniquely representing the operator

Type

int

id

String for the ID of the operator.

matrix

Matrix representation of the tensor operator in the computational basis.

Example:

Note that the returned matrix only includes explicitly declared observables making up the tensor product; that is, it only returns the matrix for the specified subsystem it is defined for.

>>> O = qml.PauliZ(0) @ qml.PauliZ(2)
>>> O.matrix
array([[ 1,  0,  0,  0],
[ 0, -1,  0,  0],
[ 0,  0, -1,  0],
[ 0,  0,  0,  1]])


To get the full $$2^3\times 2^3$$ Hermitian matrix acting on the 3-qubit system, the identity on wire 1 must be explicitly included:

>>> O = qml.PauliZ(0) @ qml.Identity(1) @ qml.PauliZ(2)
>>> O.matrix
array([[ 1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
[ 0., -1.,  0., -0.,  0., -0.,  0., -0.],
[ 0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.],
[ 0., -0.,  0., -1.,  0., -0.,  0., -0.],
[ 0.,  0.,  0.,  0., -1., -0., -0., -0.],
[ 0., -0.,  0., -0., -0.,  1., -0.,  0.],
[ 0.,  0.,  0.,  0., -0., -0., -1., -0.],
[ 0., -0.,  0., -0., -0.,  0., -0.,  1.]])

Returns

matrix representation

Return type

array

name

All constituent observable names making up the tensor product.

Returns

list containing all observable names

Return type

list[str]

non_identity_obs

Returns the non-identity observables contained in the tensor product.

Returns

list containing the non-identity observables in the tensor product

Return type

list[Observable]

num_params

Raw parameters of all constituent observables in the tensor product.

Returns

flattened list containing all dependent parameters

Return type

list[Any]

num_wires

Number of wires the tensor product acts on.

Returns

number of wires

Return type

int

parameters

Evaluated parameter values of all constituent observables in the tensor product.

Returns

nested list containing the parameters per observable in the tensor product

Return type

list[list[Any]]

return_type = None
tensor = True
wires

All wires in the system the tensor product acts on.

Returns

wires addressed by the observables in the tensor product

Return type

Wires

 compare(other) Compares with another Hamiltonian, Tensor, or Observable, to determine if they are equivalent. Decomposes this operator into products of other operators. decomposition(*params, wires) Defines a decomposition of this operator into products of other operators. Return the gate set that diagonalizes a circuit according to the specified tensor observable. label([decimals, base_label]) How the operator is represented in diagrams and drawings. Returns a pruned tensor product of observables by removing Identity instances from the observables building up the Tensor. queue([context, init]) Append the operator to the Operator queue. sparse_matrix([wires]) Computes a scipy.sparse.coo_matrix representation of this Tensor.
compare(other)

Compares with another Hamiltonian, Tensor, or Observable, to determine if they are equivalent.

Observables/Hamiltonians are equivalent if they represent the same operator (their matrix representations are equal), and they are defined on the same wires.

Warning

The compare method does not check if the matrix representation of a Hermitian observable is equal to an equivalent observable expressed in terms of Pauli matrices. To do so would require the matrix form of Hamiltonians and Tensors be calculated, which would drastically increase runtime.

Returns

True if equivalent.

Return type

(bool)

Examples

>>> ob1 = qml.PauliX(0) @ qml.Identity(1)
>>> ob2 = qml.Hamiltonian([1], [qml.PauliX(0)])
>>> ob1.compare(ob2)
True
>>> ob1 = qml.PauliX(0)
>>> ob2 = qml.Hermitian(np.array([[0, 1], [1, 0]]), 0)
>>> ob1.compare(ob2)
False

decompose()

Decomposes this operator into products of other operators.

Returns

list[Operation]

static decomposition(*params, wires)

Defines a decomposition of this operator into products of other operators.

Parameters
• params (tuple[float, int, array]) – operator parameters

• wires (Union(Sequence[int], Wires)) – wires the operator acts on

Returns

list[Operation]

diagonalizing_gates()[source]

Return the gate set that diagonalizes a circuit according to the specified tensor observable.

This method uses pre-stored eigenvalues for standard observables where possible and stores the corresponding eigenvectors from the eigendecomposition.

Returns

list containing the gates diagonalizing the tensor observable

Return type

list

label(decimals=None, base_label=None)[source]

How the operator is represented in diagrams and drawings.

Parameters
• decimals=None (Int) – If None, no parameters are included. Else, how to round the parameters.

• base_label=None (Iterable[str]) – overwrite the non-parameter component of the label. Must be same length as obs attribute.

Returns

label to use in drawings

Return type

str

>>> T = qml.PauliX(0) @ qml.Hadamard(2)
>>> T.label()
'X⊗H'
>>> T.label(base_label=["X0", "H2"])
'X0⊗H2'

prune()[source]

Returns a pruned tensor product of observables by removing Identity instances from the observables building up the Tensor.

The return_type attribute is preserved while pruning.

If the tensor product only contains one observable, then this observable instance is returned.

Note that, as a result, this method can return observables that are not a Tensor instance.

Example:

Pruning that returns a Tensor:

>>> O = qml.PauliZ(0) @ qml.Identity(1) @ qml.PauliZ(2)
>>> O.prune()
<pennylane.operation.Tensor at 0x7fc1642d1590
>>> [(o.name, o.wires) for o in O.prune().obs]
[('PauliZ', [0]), ('PauliZ', [2])]


Pruning that returns a single observable:

>>> O = qml.PauliZ(0) @ qml.Identity(1)
>>> O_pruned = O.prune()
>>> (O_pruned.name, O_pruned.wires)
('PauliZ', [0])

Returns

the pruned tensor product of observables

Return type

Observable

queue(context=<class 'pennylane.queuing.QueuingContext'>, init=False)[source]

Append the operator to the Operator queue.

sparse_matrix(wires=None)[source]

Computes a scipy.sparse.coo_matrix representation of this Tensor.

This is useful for larger qubit numbers, where the dense matrix becomes very large, while consisting mostly of zero entries.

Parameters

wires (Iterable) – Wire labels that indicate the order of wires according to which the matrix is constructed. If not provided, self.wires is used.

Returns

sparse matrix representation

Return type

scipy.sparse.coo_matrix

Example

Consider the following tensor:

>>> t = qml.PauliX(0) @ qml.PauliZ(1)


Without passing wires, the sparse representation is given by:

>>> print(t.sparse_matrix())
(0, 2)  1
(1, 3)  -1
(2, 0)  1
(3, 1)  -1


If we define a custom wire ordering, the matrix representation changes accordingly: >>> print(t.sparse_matrix(wires=[1, 0])) (0, 1) 1 (1, 0) 1 (2, 3) -1 (3, 2) -1

We can also enforce implicit identities by passing wire labels that are not present in the consituent operations:

>>> res = t.sparse_matrix(wires=[0, 1, 2])
>>> print(res.shape)
(8, 8)