pyquil
pyquil copied to clipboard
Error in Sampling on wavefunction simulator
Pre-Report Checklist
- [X] I am running the latest versions of pyQuil and the Forest SDK
- [X] I checked to make sure that this bug has not already been reported
Issue Description
The sample_bitstrings(n) of the wave function is not working as expected. Even when tried sampling with 100 samples as well as the 1000 samples the wave function provides the same state bitstrings. I was building a testing framework and found these issue when i had to compare results between different compilers.
How to Reproduce
Code Snippet
from pyquil import Program
from pyquil.api import get_qc
from pyquil.api import WavefunctionSimulator
def result_dict(resarray):
# Function to provide result array as counts dictionary
res_dict = {}
for val in resarray:
key = "".join([str(v) for v in val])
res_dict[key] = res_dict.get(key, 0) + 1
return res_dict
qc = get_qc("9q-square-qvm")
wf_sim = WavefunctionSimulator()
prog = Program('''DECLARE ro BIT[2]
H 0
MEASURE 1 ro[1]
MEASURE 0 ro[0]
''') # Simple program with H gate
result = qc.run(qc.compile(prog).wrap_in_numshots_loop(1000))
print(result_dict(result.readout_data.get("ro")))
print(result_dict(wf_sim.wavefunction(prog).sample_bitstrings(1000))) # Gives only one state
print(result_dict(wf_sim.run_and_measure(prog, trials=1000))) # Gives only one state
Error Output
{'00': 501, '10': 499}
{'00': 1000}
{'00': 1000}
If i manually run the loop and measure 100 times it works but the number of requests hitting the QVM slows it down. This was my temporary fix which i am working with.
results = []
for i in range(100):
results.append(wf_sim.run_and_measure(prog, qubits=sorted(get_measured_qubits(prog)))[0])
print(result_dict(results))
Environment Context
Operating System:
Python Version (python -V): 3.11.9
Quilc Version (quilc --version): 1.23.0
QVM Version (qvm --version): 1.17.1