jcmgray / quimb

A python library for quantum information and many-body calculations including tensor networks.
http://quimb.readthedocs.io
Other
467 stars 107 forks source link

sample function returns TypeError #97

Closed ChienKaiMa closed 2 years ago

ChienKaiMa commented 2 years ago

I found similar error messages in #96 's comment, but I think I should open a new issue for it.

Problem

I tried to sample several random quantum circuits (reduced to depth=6), from 4x4, 4x5, 5x5 to 9x10 qubits. My program simulator/quimb_qasm_sim.py behaved as I wished with circuits under 64 qubits, but it returned the following message when the qubit number is greater than 64:

Traceback (most recent call last):
  File "simulator/quimb_qasm_sim.py", line 59, in <module>
    main()
  File "simulator/quimb_qasm_sim.py", line 39, in main
    for i in circ.sample(runs, backend=backend):
  File "/home/<my-user-name>/.local/lib/python3.8/site-packages/quimb/tensor/circuit.py", line 2068, in sample
    p = self.compute_marginal(
  File "/home/<my-user-name>/.local/lib/python3.8/site-packages/quimb/tensor/circuit.py", line 1842, in compute_marginal
    p_marginal /= nfact
TypeError: ufunc 'true_divide' output (typecode 'O') could not be coerced to provided output parameter (typecode 'f') according to the casting rule ''same_kind''

Other details (may or may not be useful)

ChienKaiMa commented 2 years ago

I found solution to this issue by modifying the code from p_marginal /= nfact to p_marginal = p_marginal / nfact. I think I'll open a pull request for that.

References

jcmgray commented 2 years ago

Thanks, looks like the problem indeed is when some normalization factor is >= 2**64, it needs to be cast as a 'object' by numpy. Will take a look at PR momentarily.

Regarding the difference between sampling and 'simulation' - sampling is just one of many types of simulation. In fact, classically speaking, it's often somehow the least useful as you are intentionally introducing noise - but it also closest to mimicking the output of a real quantum computer. Also, for certain optimization problems (e.g. QAOA), once you have tuned your circuit you also the need to 'sample' candidate bitstring solutions, for which e.g. just computing local expectations is insufficient, though doing this in a perfectly unbiased way like Circuit.sample is probably overkill too.