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

simulate_expectation_values: qsimcirq outputs wrong result when the Pauli sum contains an identity Pauli string #626

Closed
rht opened this issue Sep 27, 2023 · 3 comments

Comments

@rht
Copy link

rht commented Sep 27, 2023

This is my code to test the problem:

import cirq
import qsimcirq
from qiskit import QuantumCircuit
from qiskit.primitives import Estimator
from qiskit.quantum_info import SparsePauliOp

qsim_simulator = qsimcirq.QSimSimulator()
cirq_simulator = cirq.Simulator()

q0, q1 = cirq.LineQubit.range(2)
circuit = cirq.Circuit(cirq.ry(0.3).on(q0), cirq.I(q1))

first_term = cirq.Z(q0) * cirq.Z(q1) * (0.09088576828865239 + 0j)
pauli_sum = first_term + cirq.I(q0) * cirq.I(
    q1
) * (0.2324352184350918 + 0j)
print("Pauli sum", pauli_sum)

result_cirq = cirq_simulator.simulate_expectation_values(circuit, pauli_sum)
print("Cirq", result_cirq)
print("Cirq only first term", cirq_simulator.simulate_expectation_values(circuit, first_term))
result_qsim = qsim_simulator.simulate_expectation_values(circuit, pauli_sum)
print("qsim", result_qsim)

estimator = Estimator()
qc = QuantumCircuit(2)
qc.ry(0.3, 0)
pauli_sum = SparsePauliOp.from_list([("ZZ", 0.09088576828865239 + 0j), ("II", 0.2324352184350918 + 0j)])
result_qiskit = estimator.run(qc, pauli_sum).result().values
print("Qiskit", result_qiskit)

Output:

Pauli sum 0.091*Z(0)*Z(1)+0.232*I
Cirq [(0.31926170580659224+0j)]
Cirq only first term [(0.08682648737150046+0j)]
qsim [(0.08682648737150046+0j)]
Qiskit [0.31926171]

Cirq's simulator output is consistent with Qiskit's. While qsim's output is as if the 0.232*I term is nonexistent.

@rht rht changed the title simulate_expectation_values: qsimcirq outputs wrong result when one qubit is left intact / unused simulate_expectation_values: qsimcirq outputs wrong result when the Pauli sum contains an identity Pauli string Sep 27, 2023
@rht
Copy link
Author

rht commented Sep 27, 2023

I tested with a new circuit, circuit = cirq.Circuit(cirq.ry(0.3).on(q0), cirq.ry(0.2).on(q1)), where all qubits are used. Even with this, qsim's result ignores 0.232*I.

@rht
Copy link
Author

rht commented Sep 27, 2023

This is on qsimcirq==0.13.3. Sorry, I'm still on an outdated qsimcirq because of the RAM requirement of the latest qsimcirq (#612). On latest qsim, this is no longer a problem.

@rht rht closed this as completed Sep 27, 2023
@95-martin-orion
Copy link
Collaborator

Thanks for confirming! For reference, I believe this was fixed in #577, #578, and #588. Versions before v0.16.0 may experience this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants