Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: expectation of imaginary qubit_op with desired meas result #278

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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