quil-lang / qvm

The high-performance and featureful Quil simulator.
Other
413 stars 57 forks source link

Possible a logical issue of qvm #277

Closed weucode closed 2 years ago

weucode commented 3 years ago

When debugging qvm:measure, I find that qvm yields the incorrect result.

Here are the source code of qvm:measure:

(defmethod measure ((qvm base-qvm) q)
  (check-type q nat-tuple-element)
  (assert (< q (number-of-qubits qvm)) (qvm q)
          "Trying to measure qubit ~D on a QVM with only ~D qubit~:P."
          q
          (number-of-qubits qvm))
  (let* ((r (random 1.0d0))
         (excited-probability (get-excited-state-probability (state qvm) q))
         (cbit (if (<= r excited-probability)
                   1
                   0)))
    ;; Force the non-deterministic measurement.
    (force-measurement cbit q (state qvm) excited-probability)
    ;; Return the qvm.
    (values qvm cbit)))

The following is the flowchart drawn according to the above code snippet.

measure eng (1)

According to the above flowchart, if we set excited-probablity = 0.0d0 and r=0.0d0, the value of cbit is 1, while in fact the correct value of cbit is 0 because the probability of |1> is 0.0d0.

This exception can be triggered as the following steps: Step 1:Replace the statement (let* ((r (random 1.0d0)) on line 7 with the statement (let* ((r (random 1)) to ensure the value of r to be 0; Step 2: Run the following test case:

 (setq *aqvm* (qvm:run-program 1 (cl-quil:parse-quil "I 0")))

 (qvm:measure *aqvm* 0)

Note that gate I is used to make sure the value of excited-probability to be 0.0d0.

We think it is likely to be a logical problem of qvm.

notmgsk commented 2 years ago

Closed by #278