-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathqcolor.py
executable file
·60 lines (45 loc) · 1.46 KB
/
qcolor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python
from pyquil.api import QVMConnection
from pyquil.quil import Program
from pyquil.gates import H
from matplotlib import pyplot as pp
from numpy import array, random, round, sqrt
import argparse
class QColor:
def __init__(self, simulate=False):
self.qvm = QVMConnection()
self.sim = simulate
def run(self, trials=65536, simulate=False):
if self.sim:
return random.random((trials, 3)).round()
else:
return self.qvm.run(
self.rgb.measure_all(), trials=trials)
@property
def rgb(self):
return Program(H(0), H(1), H(2))
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Produces a quantum image.')
parser.add_argument(
'-n', '--trials', type=int, default=65536)
parser.add_argument(
'-s', '--simulate', action='store_true')
args = parser.parse_args()
qcolor = QColor(simulate=args.simulate)
trials = qcolor.run(trials=args.trials)
r, g, b = array(trials).transpose()
rgb = array([r,g,b]).reshape((
int(round(sqrt(args.trials))), int(round(sqrt(args.trials))), 3
))
pp.subplot(121)
pp.title('Quantum Image')
pp.imshow(rgb*255)
pp.subplot(333)
pp.title('Color Channels')
pp.hist(r.cumsum(), color='r', bins=64)
pp.subplot(336)
pp.hist(g.cumsum(), color='g', bins=64)
pp.subplot(339)
pp.hist(b.cumsum(), color='b', bins=64)
pp.show()