Skip to content

Possible bug: qsimcirq with single qubit rotation gates #62

@sammcardle30

Description

@sammcardle30

Hi,

I've been looking to replace the cirq simulator in some of my programs with the fast qsimcirq backend. I've noticed some unexpected behaviour with the system wavefunction, and wanted to check whether this was something that I'm doing wrong, or if there might be a potential bug.

I've found that for a single qubit circuit, the X and H gates behave as I expect, but the Rx and Ry gates do not seem to give the correct wavefunction:

import cirq
import qsimcirq
import numpy as np

# Working example

c0 = cirq.Circuit()
q0 = cirq.NamedQubit('Test0')

c0.append(cirq.H(q0))
print(c0)
result_0 = cirq.Simulator(dtype = np.complex64).simulate(c0)
cirq_wf_0 = result_0.final_state
print('Cirq wavefunction: ', cirq_wf_0)
print()

qsim_circuit_0 = qsimcirq.QSimCircuit(cirq_circuit=c0)
print(qsim_circuit_0)
my_sim = qsimcirq.QSimSimulator()
myres_0 = my_sim.simulate(program=qsim_circuit_0)
qsim_wf_0 = myres_0._final_simulator_state
print('Qsim wavefunction: ', qsim_wf_0)

This prints:
Test0: ───H───
Cirq wavefunction:
[0.70710677+0.j
0.70710677+0.j]

Test0: ───H───
Qsim wavefunction:
[0.70710677
0.
0.70710677
0. ]

as I would expect.

# Non-working example

c1 = cirq.Circuit()
q1 = cirq.NamedQubit('Test1')

c1.append(cirq.Rx(np.pi/2)(q1))
print(c1)
result_1 = cirq.Simulator(dtype = np.complex64).simulate(c1)
cirq_wf_1 = result_1.final_state
print('Cirq wavefunction: ', cirq_wf_1)
print()

qsim_circuit_1 = qsimcirq.QSimCircuit(cirq_circuit=c1)
print(qsim_circuit_1)
my_sim = qsimcirq.QSimSimulator()
myres_1 = my_sim.simulate(program=qsim_circuit_1)
qsim_wf_1 = myres_1._final_simulator_state
print('Qsim wavefunction: ', qsim_wf_1)

This prints:
Test1: ───Rx(0.5π)───
Cirq wavefunction:
[0.70710677+0.j
0.-0.70710677j]

Test1: ───Rx(0.5π)───
Qsim wavefunction:
[ 0.5
0.5
0.5
-0.5]

While these appear inconsistent, I think this can be explained by removing the global phase from the qsim wavefunction, which yields: e^{i*pi/4} (1/sqrt(2) , -i/sqrt(2)).

However, I obtain a similar inconsistency between the cirq and qsim wavefunctions for different rotation angles, and for the Ry gate too, which don't seem to be explainable with the global phase:

# Non-working example

c1 = cirq.Circuit()
q1 = cirq.NamedQubit('Test1')

c1.append(cirq.Ry(np.pi/3)(q1))
print(c1)
result_1 = cirq.Simulator(dtype = np.complex64).simulate(c1)
cirq_wf_1 = result_1.final_state
print('Cirq wavefunction: ', cirq_wf_1)
print()


qsim_circuit_1 = qsimcirq.QSimCircuit(cirq_circuit=c1)
print(qsim_circuit_1)
my_sim = qsimcirq.QSimSimulator()
myres_1 = my_sim.simulate(program=qsim_circuit_1)
qsim_wf_1 = myres_1._final_simulator_state
print('Qsim wavefunction: ', qsim_wf_1)

This prints:
Test1: ───Ry(0.333π)───
Cirq wavefunction:
[0.8660254+0.j
0.5 +0.j]

Test1: ───Ry(0.333π)───
Qsim wavefunction:
[0.98614323
0.
0.16589613
0. ]
I'm not sure how to reconcile these two wavefunctions, so would really appreciate any insights that you might have on this.

Thank you in advance for your help with these questions.

Best regards,

Sam

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions