Skip to content

Commit

Permalink
Merge pull request #153 from mpharrigan/use-namedtuple
Browse files Browse the repository at this point in the history
Use namedtuple for readability
  • Loading branch information
ampolloreno committed May 24, 2018
2 parents 8096e35 + 84dc40d commit d8a6d33
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions grove/measurements/estimation.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""
Utilities for estimating expected values of Pauli terms given pyquil programs
"""
from collections import namedtuple

import numpy as np
from pyquil.paulis import (PauliSum, PauliTerm, commuting_sets, sI,
term_with_coeff, is_identity, is_zero)
Expand Down Expand Up @@ -97,6 +99,10 @@ def get_parity(pauli_terms, bitstring_results):
return results


EstimationResult = namedtuple('EstimationResult',
('expected_value', 'covariance', 'variance', 'n_shots'))


def estimate_pauli_sum(pauli_terms, basis_transform_dict, program,
variance_bound, quantum_resource,
commutation_check=True):
Expand Down Expand Up @@ -172,11 +178,12 @@ def estimate_pauli_sum(pauli_terms, basis_transform_dict, program,

# calculate the expected values....
covariance_mat = np.cov(results)
sample_variance = coeff_vec.T.dot(covariance_mat).dot(coeff_vec) / \
results.shape[1]
sample_variance = coeff_vec.T.dot(covariance_mat).dot(coeff_vec) / results.shape[1]

return coeff_vec.T.dot(np.mean(results, axis=1)), covariance_mat, \
sample_variance, results.shape[1]
return EstimationResult(expected_value=coeff_vec.T.dot(np.mean(results, axis=1)),
covariance=covariance_mat,
variance=sample_variance,
n_shots=results.shape[1])


def remove_identity(psum):
Expand Down Expand Up @@ -236,8 +243,8 @@ def estimate_locally_commuting_operator(program, pauli_sum,
quantum_resource,
commutation_check=False)

expected_value += results[0].real
total_shots += results[3]
estimator_variance += results[2]
expected_value += results.expected_value.real
total_shots += results.n_shots
estimator_variance += results.variance

return expected_value, estimator_variance, total_shots

0 comments on commit d8a6d33

Please sign in to comment.