Skip to content

Commit

Permalink
Clean up earlier expectation from samples code
Browse files Browse the repository at this point in the history
Also fixes documentation line in `pyproject.toml`
  • Loading branch information
chmwzc committed Nov 7, 2023
1 parent d998987 commit 56a3aa7
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 96 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ authors = ["The Qibo team"]
license = "Apache License 2.0"
readme = "README.md"
repository = "https://github.com/qiboteam/qibochem/"
documentation = ""
documentation = "https://qibo.science/qibochem/latest/"
keywords = []
classifiers = [
"Programming Language :: Python :: 3",
Expand Down
27 changes: 0 additions & 27 deletions src/qibochem/measurement/basis_rotate.py

This file was deleted.

69 changes: 1 addition & 68 deletions src/qibochem/measurement/expectation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,73 +3,6 @@
from qibo.hamiltonians import SymbolicHamiltonian
from qibo.symbols import Z

from qibochem.measurement.basis_rotate import measure_rotate_basis


def pauli_expectation_shots(qc, nshots):
"""
Calculates expectation value of circuit for pauli string from shots
Args:
qc: quantum circuit with appropriate rotations and measure gates
nshots: number of shots
Returns:
expectation: expectation value
"""
result = qc.execute(nshots=nshots)
meas = result.frequencies(binary=True)

bitcount = 0
for bitstring, count in meas.items():
if bitstring.count("1") % 2 == 0:
bitcount += count
else:
bitcount -= count
expectation = bitcount / nshots
return expectation


def circuit_expectation_shots(qc, of_qubham, nshots=1000):
"""
Calculates expectation value of qubit hamiltonian w.r.t. circuit ansatz using shots
Args:
qc: Quantum circuit with ansatz
of_qubham: Molecular Hamiltonian given as an OpenFermion QubitOperator
Returns:
Expectation value of of_qubham w.r.t. circuit ansatz
"""
# nterms = len(of_qubham.terms)
nqubits = qc.nqubits
# print(nterms)
coeffs = []
strings = []

expectation = 0
for qubop in of_qubham.terms:
coeffs.append(of_qubham.terms[qubop])
strings.append([qubop])

istring = 0
for pauli_op in strings:
if len(pauli_op[0]) == 0: # no pauli obs to measure,
expectation += coeffs[istring]
# print(coeffs[istring])
else:
# add rotation gates to rotate pauli observable to computational basis
# i.e. X to Z, Y to Z
meas_qc = measure_rotate_basis(pauli_op[0], nqubits)
full_qc = qc + meas_qc
# print(full_qc.draw())
pauli_op_exp = pauli_expectation_shots(full_qc, nshots)
expectation += coeffs[istring] * pauli_op_exp
## print(pauli_op, pauli_op_exp, coeffs[istring])
istring += 1

return expectation


def expectation(
circuit: qibo.models.Circuit, hamiltonian: SymbolicHamiltonian, from_samples=False, n_shots=1000
Expand All @@ -82,7 +15,7 @@ def expectation(
circuit (qibo.models.Circuit): Quantum circuit ansatz
hamiltonian (SymbolicHamiltonian): Molecular Hamiltonian
from_samples: Whether the expectation value calculation uses samples or the simulated
state vector. Default: False, state vector simulation
state vector. Default: False; Results are from a state vector simulation
n_shots: Number of times the circuit is run for the from_samples=True case
Returns:
Expand Down

0 comments on commit 56a3aa7

Please sign in to comment.