# qml.utils.sparse_hamiltonian¶

sparse_hamiltonian(H, wires=None)[source]

Warning: This method is deprecated. Use :meth:~.Hamiltonian.sparse_matrix instead.

Computes the sparse matrix representation a Hamiltonian in the computational basis.

Parameters
• H (Hamiltonian) – Hamiltonian operator for which the matrix representation should be computed

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

Returns

a sparse matrix in scipy Compressed Sparse Row (CSR) format with dimension $$(2^n, 2^n)$$, where $$n$$ is the number of wires

Return type

csr_matrix

Example:

This function can be used by passing a qml.Hamiltonian object as:

>>> coeffs = [1, -0.45]
>>> obs = [qml.PauliZ(0) @ qml.PauliZ(1), qml.PauliY(0) @ qml.PauliZ(1)]
>>> H = qml.Hamiltonian(coeffs, obs)
>>> H_sparse = sparse_hamiltonian(H)
>>> H_sparse
<4x4 sparse matrix of type '<class 'numpy.complex128'>'
with 2 stored elements in COOrdinate format>


The resulting sparse matrix can be either used directly or transformed into a numpy array:

>>> H_sparse.toarray()
array([[ 1.+0.j  ,  0.+0.j  ,  0.+0.45j,  0.+0.j  ],
[ 0.+0.j  , -1.+0.j  ,  0.+0.j  ,  0.-0.45j],
[ 0.-0.45j,  0.+0.j  , -1.+0.j  ,  0.+0.j  ],
[ 0.+0.j  ,  0.+0.45j,  0.+0.j  ,  1.+0.j  ]])
`

Using PennyLane

Development

API

Internals