pedrorrivero / qrand

A multiprotocol and multiplatform quantum random number generation framework
https://pypi.org/project/qrand/
Apache License 2.0
26 stars 14 forks source link

Simulator backend not randomized. #30

Open gatorwatt opened 2 years ago

gatorwatt commented 2 years ago

Describe the bug

Hello there. I have cited your library in a paper currently under peer review. I am trying to validate use of Qrand with simulator backend on qiskit client. The sampling does not appear to return random values, although it works with a live circuit backend.

To Reproduce

from qrand import QuantumBitGenerator
from qrand.platforms import QiskitPlatform
from qrand.protocols import HadamardProtocol
from qiskit import IBMQ

provider = IBMQ.load_account()
backend = provider.get_backend('ibmq_qasm_simulator')

#I added a backend assignment here and it allowed it to compile the cell
#it worked with a live circuit
#so I am assuming that backend assignment is supported functionality

platform = QiskitPlatform(provider, backend)
protocol = HadamardProtocol()
bitgen = QuantumBitGenerator(platform, protocol)

#and then to validate tried sampling 5 points from numpy's standard_normal
import numpy as np
from numpy.random import Generator
gen = Generator(bitgen)
gen.standard_normal(5)

#the array returned 5 equal values

array([-7.42778876e-09, -7.42778876e-09, -7.42778876e-09, -7.42778876e-09, -7.42778876e-09])

It also generated a bunch of qiskit printouts as follows (repeated for each sampled number):

Exception ignored on calling ctypes callback function: <function QuantumBitGenerator._next_64.<locals>.next_64 at 0x7f79a8f8eee0>
Traceback (most recent call last):
  File "/Users/nicholasteague/opt/anaconda3/envs/qiskit/lib/python3.9/site-packages/qrand/quantum_bit_generator.py", line 311, in next_64
    return uint64(self.random_uint(64))
  File "/Users/nicholasteague/opt/anaconda3/envs/qiskit/lib/python3.9/site-packages/qrand/quantum_bit_generator.py", line 259, in random_uint
    return int(self.random_bitstring(num_bits), base=2)
  File "/Users/nicholasteague/opt/anaconda3/envs/qiskit/lib/python3.9/site-packages/qrand/quantum_bit_generator.py", line 207, in random_bitstring
    self._refill_cache()
  File "/Users/nicholasteague/opt/anaconda3/envs/qiskit/lib/python3.9/site-packages/qrand/quantum_bit_generator.py", line 277, in _refill_cache
    bitstring: str = self.platform.fetch_random_bits(self.protocol)
  File "/Users/nicholasteague/opt/anaconda3/envs/qiskit/lib/python3.9/site-packages/qrand/platforms/qiskit/platform.py", line 117, in fetch_random_bits
    result: ProtocolResult = protocol.run(self)
  File "/Users/nicholasteague/opt/anaconda3/envs/qiskit/lib/python3.9/site-packages/qrand/protocols/hadamard.py", line 88, in run
    circuit: QuantumCircuit = factory.create_circuit(num_qubits)
  File "/Users/nicholasteague/opt/anaconda3/envs/qiskit/lib/python3.9/site-packages/qrand/platforms/qiskit/platform.py", line 105, in create_circuit
    return QiskitCircuit(num_qubits)
  File "/Users/nicholasteague/opt/anaconda3/envs/qiskit/lib/python3.9/site-packages/qrand/platforms/qiskit/circuit.py", line 33, in __init__
    super(QuantumCircuit, self).__init__(num_qubits, num_qubits)
  File "/Users/nicholasteague/opt/anaconda3/envs/qiskit/lib/python3.9/site-packages/qiskit/circuit/quantumcircuit.py", line 226, in __init__
    raise CircuitError(
qiskit.circuit.exceptions.CircuitError: "Circuit args must be Registers or integers. (['str', 'str'] '('32', '32')' was provided)"

Expected behavior

I expected the sampling to generate 5 unique numbers when running the simulator backend

Screenshots

Desktop (please complete the following information):

Additional context

This might be related. I was running some code from a qiskit book and it appears that some kind of convention may have changed for their library in a recent update with respect to representing qubit counts .configuration().n_qubits for various backends as strings instead of integers. Wondering if a simple type conversion somewhere could resolve .