From 56a3aa7c39330e629f6485b4cf1bd3ea2fa0bf0d Mon Sep 17 00:00:00 2001 From: Wong Zi Cheng <70616433+chmwzc@users.noreply.github.com> Date: Tue, 7 Nov 2023 07:09:05 +0000 Subject: [PATCH] Clean up earlier expectation from samples code Also fixes documentation line in `pyproject.toml` --- pyproject.toml | 2 +- src/qibochem/measurement/basis_rotate.py | 27 ---------- src/qibochem/measurement/expectation.py | 69 +----------------------- 3 files changed, 2 insertions(+), 96 deletions(-) delete mode 100644 src/qibochem/measurement/basis_rotate.py diff --git a/pyproject.toml b/pyproject.toml index 7b40d51..16ccd25 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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", diff --git a/src/qibochem/measurement/basis_rotate.py b/src/qibochem/measurement/basis_rotate.py deleted file mode 100644 index 69ba37f..0000000 --- a/src/qibochem/measurement/basis_rotate.py +++ /dev/null @@ -1,27 +0,0 @@ -import qibo -from qibo import gates -from qibo.models import Circuit - - -def measure_rotate_basis(pauli_op, nqubits): - """ - rotate each qubit to computational basis (Z-basis) for measurement - input: - pauli_op: qubit operator - nqubits: number of qubits - output: - qibo circuit with rotations and measurement gates - """ - - mqc = Circuit(nqubits) - for p in pauli_op: - if p[1] == "Z": - mqc.add(gates.M(p[0])) - elif p[1] == "Y": - mqc.add(gates.S(p[0]).dagger()) - mqc.add(gates.H(p[0])) - mqc.add(gates.M(p[0])) - elif p[1] == "X": - mqc.add(gates.H(p[0])) - mqc.add(gates.M(p[0])) - return mqc diff --git a/src/qibochem/measurement/expectation.py b/src/qibochem/measurement/expectation.py index 48e7d88..385a063 100644 --- a/src/qibochem/measurement/expectation.py +++ b/src/qibochem/measurement/expectation.py @@ -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 @@ -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: