Skip to content

Pybind11 error: Unable to cast Python instance of type <class 'qsimcirq.qsim_avx2.Circuit'> to C++ type '?' #870

@YingrongChen

Description

@YingrongChen

Describe the issue

When running gpu version of qsimcirq (installed from source following the tutorial GPU-based quantum simulation on Google Cloud) in a Python virtual environment, I get the following runtime error:

RuntimeError: Unable to cast Python instance of type <class 'qsimcirq.qsim_avx2.Circuit'> to C++ type '?' (#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for details)

This happens when both qsim_avx2 and qsim_cuda backends are present in site-packages. The error occurs while calling compute_amplitudes or simulate on a qsimcirq.QSimSimulator instance.

Removing one of the backend .so files makes the error go away, but then the package fails to import if the missing backend is referenced in init.py.

What version of qsim are you using?

0.23.0.dev0

How can the issue be reproduced?

  1. In a Python 3.12 venv, install qsimcirq from source (following the tutorial GPU-based quantum simulation on Google Cloud
    )
  2. Create a simple circuit in Cirq and try to run it with qsimcirq.QSimSimulator.
  3. Call simulate or compute_amplitudes. The error appears.

Example code:

import cirq, qsimcirq
q = cirq.LineQubit.range(2)
c = cirq.Circuit([cirq.H(q[0]), cirq.CX(q[0], q[1])])
sim = qsimcirq.QSimSimulator()
result = sim.simulate(c)

Metadata

Metadata

Assignees

Labels

area/c++Involves the C++ code portions of qsimarea/gpuInvolves CUDA, GPUs, and similarpriority/p1High priority

Type

Projects

Status

No status

Relationships

None yet

Development

No branches or pull requests

Issue actions