quantumlib / qsim

Schrödinger and Schrödinger-Feynman simulators for quantum circuits.
Apache License 2.0
454 stars 151 forks source link

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

Closed rht closed 1 year ago

rht commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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.

95-martin-orion commented 1 year ago

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