Skip to content

Commit a459d85

Browse files
committed
added first attempt at quantump integrator
1 parent 6d4a7d8 commit a459d85

File tree

3 files changed

+108
-1
lines changed

3 files changed

+108
-1
lines changed

examples/quantum_mc.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""
2+
Run integration with qibo as the backend
3+
"""
4+
5+
from vegasflow.quantum import quantum_wrapper
6+
from vegasflow import float_me
7+
import time
8+
import numpy as np
9+
import tensorflow as tf
10+
11+
12+
# MC integration setup
13+
dim = 2
14+
ncalls = int(1e2)
15+
n_iter = 5
16+
17+
18+
def symgauss(xarr):
19+
"""symgauss test function"""
20+
n_dim = xarr.shape[-1]
21+
a = float_me(0.1)
22+
n100 = float_me(100 * n_dim)
23+
pref = tf.pow(1.0 / a / np.sqrt(np.pi), n_dim)
24+
coef = tf.reduce_sum(tf.range(n100 + 1))
25+
coef += tf.reduce_sum(tf.square((xarr - 1.0 / 2.0) / a), axis=1)
26+
coef -= (n100 + 1) * n100 / 2.0
27+
return pref * tf.exp(-coef)
28+
29+
30+
if __name__ == "__main__":
31+
"""Testing several different integrations"""
32+
print(f"VEGAS MC, ncalls={ncalls}:")
33+
start = time.time()
34+
result = quantum_wrapper(symgauss, dim, n_iter, ncalls)
35+
end = time.time()

pyproject.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ dependencies = [
2424
]
2525

2626
[project.optional-dependencies]
27+
quantum = [
28+
"qibolab[qrng] @ git+https://github.com/qiboteam/qibolab@qrng"
29+
]
2730
docs = [
2831
'sphinx_rtd_theme',
2932
'recommonmark',
@@ -55,4 +58,3 @@ profile = "black" # https://black.readthedocs.io/en/stable/guides/using_black_wi
5558
skip_gitignore = true
5659
known_first_party = ["bgtrees"]
5760
force_sort_within_sections = true
58-

src/vegasflow/quantum.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
"""
2+
A Monte Carlo integrator built upon Qibo for quantum integration
3+
"""
4+
5+
from .monte_carlo import wrapper, sampler
6+
from .plain import PlainFlow # start building upon a naive idiotic integrator
7+
from .configflow import run_eager, DTYPE
8+
import tensorflow as tf
9+
10+
11+
class QuantumIntegrator(PlainFlow):
12+
"""
13+
Simple Monte Carlo integrator.
14+
"""
15+
16+
_CAN_RUN_VECTORIAL = False
17+
18+
def __init__(self, *args, **kwargs):
19+
# This integrator can only run for now in eager mode and needs qibolab to be installed
20+
run_eager(True)
21+
22+
try:
23+
from qibolab.instruments.qrng import QRNG
24+
from serial.serialutil import SerialException
25+
except ModuleNotFoundError as e:
26+
raise ModuleNotFoundError("You can do pip install vegasflow[quantum]") from e
27+
28+
try:
29+
qrng = QRNG(address="/dev/ttyACM0")
30+
qrng.connect()
31+
except SerialException as e:
32+
raise SerialException("No quantum device found") from e
33+
34+
self._quantum_sampler = qrng
35+
super().__init__(*args, **kwargs)
36+
37+
def run_integration(self, *args, **kwargs):
38+
ret = super().run_integration(*args, **kwargs)
39+
self._quantum_sampler.disconnect()
40+
return ret
41+
42+
def _generate_random_array(self, n_events, *args):
43+
"""
44+
Returns
45+
-------
46+
`rnds`: array of (n_events, n_dim) random points
47+
`idx` : index associated to each random point
48+
`wgt` : wgt associated to the random point
49+
"""
50+
quantum_rnds_raw = self._quantum_sampler.random((n_events, self.n_dim))
51+
rnds_raw = tf.cast(quantum_rnds_raw, dtype=DTYPE)
52+
53+
rnds, wgts_raw, *extra = self._digest_random_generation(rnds_raw, *args)
54+
55+
wgts = wgts_raw * self.xjac
56+
if self._xdelta is not None:
57+
# Now apply integration limits
58+
rnds = self._xmin + rnds * self._xdelta
59+
wgts *= self._xdeltajac
60+
return rnds, wgts, *extra
61+
62+
63+
def quantum_wrapper(*args, **kwargs):
64+
"""Wrapper around QuantumIntegrator"""
65+
return wrapper(QuantumIntegrator, *args, **kwargs)
66+
67+
68+
def quantum_sampler(*args, **kwargs):
69+
"""Wrapper sampler around QuantumIntegrator"""
70+
return sampler(QuantumIntegrator, *args, **kwargs)

0 commit comments

Comments
 (0)