Computing custom observables involve superpositon in Pennylane

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

def H(wires):
P0 = qml.Projector(np.array([0, 0, 0, 0]), wires=wires)
P1 = qml.Projector(1/np.sqrt(2) * np.array([0, 0, 1, 1]) + 1/np.sqrt(2) * np.array([1, 1, 0, 0]), wires=wires)
return qml.Hamiltonian([1.0, 1.0], [P0, P1], simplify=False)

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]

How can I implement this observable?

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

You could also print H to see the decomposition :rocket: