I want to compute the expectation value of |0000><0000| + 1/2 * (|0011> + |1100>) (<0011| + <1100|) in pennylane. I construct the custom observable in this way
But I got an error message:
ValueError: Basis state must only consist of 0s and 1s; got [0.7071067811865475, 0.7071067811865475, 0.7071067811865475, 0.7071067811865475]
Hey!
In this case, the matrix is easy to generate so you can use the pauli_decompose function to get a Pauli representation of the matrix Look this example:
import pennylane as qml
from pennylane import numpy as np
def H(wires):
matrix = np.zeros([2 ** 4, 2 ** 4])
matrix[0,0] = 1 # |0000><0000|
matrix[3,3] = 1/2 # |0011><0011|
matrix[3,12] = 1/2 # |0011><1100|
matrix[12,3] = 1/2 # |1100><0011|
matrix[12,12] = 1/2 # |1100><1100|
H = qml.pauli.pauli_decompose(matrix, wire_order = wires)
return H
dev = qml.device("default.qubit", wires = 4)
@qml.qnode(dev)
def circuit():
# your ansatz
return qml.expval(H(wires = range(4)))
circuit()