Skip to content

Commit

Permalink
fix error for imaginary qubit_op with desired meas result (#278)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesB-1qbit authored Feb 17, 2023
1 parent 0809250 commit d9f788b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
6 changes: 4 additions & 2 deletions tangelo/linq/target/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,10 @@ def get_expectation_value(self, qubit_operator, state_prep_circuit, initial_stat
qb_op_real.terms[term], qb_op_imag.terms[term] = coef.real, coef.imag
qb_op_real.compress()
qb_op_imag.compress()
exp_real = self.get_expectation_value(qb_op_real, state_prep_circuit, initial_statevector=initial_statevector)
exp_imag = self.get_expectation_value(qb_op_imag, state_prep_circuit, initial_statevector=initial_statevector)
exp_real = self.get_expectation_value(qb_op_real, state_prep_circuit, initial_statevector=initial_statevector,
desired_meas_result=desired_meas_result)
exp_imag = self.get_expectation_value(qb_op_imag, state_prep_circuit, initial_statevector=initial_statevector,
desired_meas_result=desired_meas_result)
return exp_real if (exp_imag == 0.) else exp_real + 1.0j * exp_imag

def get_variance(self, qubit_operator, state_prep_circuit, initial_statevector=None, desired_meas_result=None):
Expand Down
2 changes: 1 addition & 1 deletion tangelo/linq/tests/test_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ def test_get_exp_value_from_statevector_with_shots_h2(self):

def test_get_exp_value_mixed_state_desired_measurement_with_shots(self):
""" Get expectation value of mixed state by post-selecting on desired measurement."""
qubit_operator = QubitOperator("X0 X1") + QubitOperator("Y0 Y1") + QubitOperator("Z0 Z1")
qubit_operator = QubitOperator("X0 X1") + QubitOperator("Y0 Y1") + QubitOperator("Z0 Z1") + QubitOperator("X0 Y1", 1j)

ham = get_sparse_operator(qubit_operator).toarray()
exact_sv = np.array([0.+0.j, 0.+0.j, 0.87758256+0.j, -0.47942554+0.j])
Expand Down

0 comments on commit d9f788b

Please sign in to comment.