old_contents: import numpy as np
  from mock import patch
  
  from grove.alpha.jordan_gradient.jordan_gradient import estimate_gradient
  
  
  def test_gradient_estimator():
      test_perturbation = .25
      test_precision = 3
      test_measurements = 10
  
      with patch("pyquil.api.SyncConnection") as cxn:
          cxn.run.return_value = [[1, 0, 0] for i in range(test_measurements)]
  
      gradient_estimate = estimate_gradient(test_perturbation, test_precision,
                                            n_measurements=test_measurements,
                                            cxn=cxn)
  
      assert(np.isclose(gradient_estimate, test_perturbation))
  
new_contents: import numpy as np
  from mock import patch
  
  from pyquil.gates import X, H, CPHASE, SWAP, MEASURE
  import pyquil.quil as pq
  
  from grove.alpha.phaseestimation.phase_estimation import controlled
  from grove.alpha.jordan_gradient.jordan_gradient import gradient_program, \
      estimate_gradient
  
  
  def test_gradient_program():
      f_h = 0.25
      precision = 2
      
      trial_prog = gradient_program(f_h, precision)
      
      result_prog = pq.Program().inst([X(2), H(2), H(0), H(1)])
      
      phase_factor = np.exp(-1.0j * np.pi * f_h)
      U = np.array([[phase_factor, 0],
                    [0, phase_factor]])
      q_out = range(precision, precision+1)
      for i in range(precision):
          if i > 0:
              U = np.dot(U, U)
          cU = controlled(U)
          name = "CONTROLLED-U{0}".format(2 ** i)
          result_prog.defgate(name, cU)
          result_prog.inst((name, i) + tuple(q_out))
      
      result_prog.inst([H(1), CPHASE(1.5707963267948966, 0, 1), H(0), 
                       SWAP(0, 1), MEASURE(0, [0]), MEASURE(1, [1])])
  
      assert(trial_prog == result_prog)
  
  
  def test_estimate_gradient():
      test_perturbation = .25
      test_precision = 3
      test_measurements = 10
  
      with patch("pyquil.api.SyncConnection") as cxn:
          cxn.run.return_value = [[1, 0, 0] for i in range(test_measurements)]
  
      gradient_estimate = estimate_gradient(test_perturbation, test_precision,
                                            n_measurements=test_measurements,
                                            cxn=cxn)
  
      assert(np.isclose(gradient_estimate, test_perturbation))
  
subject: test gradient program, in the spirit of qft test