eclipse-qrisp / Qrisp

Qrisp - a framework for high-level programming of Quantum computers
https://www.qrisp.eu/
Eclipse Public License 2.0
93 stars 26 forks source link

shot handling when adding numbers #81

Closed JRGit4UE closed 2 months ago

JRGit4UE commented 2 months ago

On my Qrisp 0.4.10 when I test adding numbers, it is unclear to me how to handle the number of shots in simulation, as the result of the adder seems to always be correct, no matter if shots=1 or shots=100000.

Example
from qrisp import QuantumArray, QuantumFloat, gidney_adder, cx
import qiskit_aer as Aer  # v1.1.1

a = QuantumFloat(3)
b = QuantumFloat(3)
a[:] = 1
b[:] = 2

print(a)  # {1: 1.0}
print(b)  # {2: 1.0}

gidney_adder(a, b)

qiskit_backend = Aer.AerSimulator()
b.get_measurement(backend=VirtualQiskitBackend(qiskit_backend), shots=1)  # shots seems to be irrelevant for the measurement
print(b)  # {3: 1.0}  always correct result
positr0nium commented 2 months ago

Hi, I think there are two missunderstandings here:

  1. The script that you wrote doesn't involve any quantum randomness (1+2 = 3).
  2. print(b) will call get_measurement again but with the default simulator and 100 000 shots. If you want to see the result returned from the Qiskit backend, please use print(b.get_measurement(backend=VirtualQiskitBackend(qiskit_backend), shots=1)) The get_measurement method has no influence on the compiled circuit - instead it allows you to probe the state at that point. If you want to compile a measurement use c = measure(b). This will give you a list of classical bits.
JRGit4UE commented 2 months ago

Ok, now it's clear - thank you. @1.) Although I would have expected that randomness is inherently in every qubit I measure...

positr0nium commented 2 months ago

If you feel like it, you can also simulate noise :) For that use the noisy qiskit backends

from qrisp import QuantumArray, QuantumFloat, gidney_adder, cx, h
import qiskit_aer as Aer  # v1.1.1
from qiskit_ibm_runtime.fake_provider import FakeWashington
from qrisp.interface import VirtualQiskitBackend

a = QuantumFloat(3)
b = QuantumFloat(3)
a[:] = 1
b[:] = 2

print(a)  # {1: 1.0}
print(b)  # {2: 1.0}

b += a
qiskit_backend = FakeWashington()
print(b.get_measurement(backend=VirtualQiskitBackend(qiskit_backend), shots=10)) 
# {3: 0.4, 7: 0.3, 5: 0.2, 2: 0.1}