qml.operation.Tensor

class Tensor(*args)[source]

Bases: pennylane.operation.Observable

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.

do_check_domain

eigvals

Return the eigenvalues of the specified tensor product observable.

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.

par_domain

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

Raw parameters of all constituent observables in the tensor product.

Returns

flattened list containing all dependent parameters

Return type

list[Any]

do_check_domain = True
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]

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

par_domain = None
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

check_domain(p[, flattened])

Check the validity of a parameter.

compare(other)

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

diagonalizing_gates()

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

prune()

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

queue()

Append the operator to the Operator queue.

check_domain(p, flattened=False)

Check the validity of a parameter.

Variable instances can represent any real scalars (but not arrays).

Parameters
  • p (Number, array, Variable) – parameter to check

  • flattened (bool) – True means p is an element of a flattened parameter sequence (affects the handling of ‘A’ parameters)

Raises
  • TypeError – parameter is not an element of the expected domain

  • ValueError – parameter is an element of an unknown domain

Returns

p

Return type

Number, array, Variable

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
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

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()

Append the operator to the Operator queue.