Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

📚 The CoCalc Library - books, templates and other resources

132928 views
License: OTHER
1
#!/usr/bin/env python
2
#
3
# Copyright 2019 the original author or authors.
4
#
5
# Licensed under the Apache License, Version 2.0 (the "License");
6
# you may not use this file except in compliance with the License.
7
# You may obtain a copy of the License at
8
#
9
# http://www.apache.org/licenses/LICENSE-2.0
10
#
11
# Unless required by applicable law or agreed to in writing, software
12
# distributed under the License is distributed on an "AS IS" BASIS,
13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
# See the License for the specific language governing permissions and
15
# limitations under the License.
16
#
17
import pygame
18
from qiskit import BasicAer, QuantumRegister, ClassicalRegister, QuantumCircuit, execute
19
from qiskit.tools.visualization import plot_histogram
20
21
from vqe_playground.utils.resources import load_image
22
23
DEFAULT_NUM_SHOTS = 100
24
25
class MeasurementsHistogram(pygame.sprite.Sprite):
26
"""Displays a histogram with measurements"""
27
def __init__(self, circuit, num_shots=DEFAULT_NUM_SHOTS):
28
pygame.sprite.Sprite.__init__(self)
29
self.image = None
30
self.rect = None
31
self.set_circuit(circuit, num_shots)
32
33
# def update(self):
34
# # Nothing yet
35
# a = 1
36
37
def set_circuit(self, circuit, num_shots=DEFAULT_NUM_SHOTS):
38
backend_sim = BasicAer.get_backend('qasm_simulator')
39
qr = QuantumRegister(circuit.width(), 'q')
40
cr = ClassicalRegister(circuit.width(), 'c')
41
meas_circ = QuantumCircuit(qr, cr)
42
meas_circ.barrier(qr)
43
meas_circ.measure(qr, cr)
44
complete_circuit = circuit + meas_circ
45
46
job_sim = execute(complete_circuit, backend_sim, shots=num_shots)
47
48
result_sim = job_sim.result()
49
50
counts = result_sim.get_counts(complete_circuit)
51
print(counts)
52
53
histogram = plot_histogram(counts)
54
histogram.savefig("vqe_playground/utils/data/bell_histogram.png")
55
56
self.image, self.rect = load_image('bell_histogram.png', -1)
57
self.image.convert()
58
59