Skip to content

Commit

Permalink
feat: implemented expectation for non diagonal observbales
Browse files Browse the repository at this point in the history
  • Loading branch information
BrunoLiegiBastonLiegi committed Oct 14, 2024
1 parent e7276d8 commit e756691
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/qibo/hamiltonians/expectation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from math import prod

from qibo import Circuit, gates, symbols
from qibo.config import raise_error
from qibo.hamiltonians import SymbolicHamiltonian


def Expectation(circuit: Circuit, observable: SymbolicHamiltonian) -> float:
if len(circuit.measurements) > 0:
raise_error(RuntimeError)
exp_val = 0.0
for term in observable.terms:
Z_observable = SymbolicHamiltonian(
prod([symbols.Z(q) for q in term.target_qubits]),
nqubits=circuit.nqubits,
backend=observable.backend,
)
measurements = [
gates.M(factor.target_qubit, basis=factor.gate.__class__)
for factor in term.factors
]
circ_copy = circuit.copy(True)
[circ_copy.add(m) for m in measurements]
freq = observable.backend.execute_circuit(circ_copy).frequencies()
exp_val += Z_observable.expectation_from_samples(freq)
return exp_val

0 comments on commit e756691

Please sign in to comment.