📚 The CoCalc Library - books, templates and other resources
License: OTHER
#!/usr/bin/env python1#2# Copyright 2019 the original author or authors.3#4# Licensed under the Apache License, Version 2.0 (the "License");5# you may not use this file except in compliance with the License.6# You may obtain a copy of the License at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing, software11# distributed under the License is distributed on an "AS IS" BASIS,12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13# See the License for the specific language governing permissions and14# limitations under the License.15#16import pygame17from qiskit import BasicAer, QuantumRegister, ClassicalRegister, QuantumCircuit, execute18from qiskit.tools.visualization import plot_histogram1920from vqe_playground.utils.resources import load_image2122DEFAULT_NUM_SHOTS = 1002324class MeasurementsHistogram(pygame.sprite.Sprite):25"""Displays a histogram with measurements"""26def __init__(self, circuit, num_shots=DEFAULT_NUM_SHOTS):27pygame.sprite.Sprite.__init__(self)28self.image = None29self.rect = None30self.set_circuit(circuit, num_shots)3132# def update(self):33# # Nothing yet34# a = 13536def set_circuit(self, circuit, num_shots=DEFAULT_NUM_SHOTS):37backend_sim = BasicAer.get_backend('qasm_simulator')38qr = QuantumRegister(circuit.width(), 'q')39cr = ClassicalRegister(circuit.width(), 'c')40meas_circ = QuantumCircuit(qr, cr)41meas_circ.barrier(qr)42meas_circ.measure(qr, cr)43complete_circuit = circuit + meas_circ4445job_sim = execute(complete_circuit, backend_sim, shots=num_shots)4647result_sim = job_sim.result()4849counts = result_sim.get_counts(complete_circuit)50print(counts)5152histogram = plot_histogram(counts)53histogram.savefig("vqe_playground/utils/data/bell_histogram.png")5455self.image, self.rect = load_image('bell_histogram.png', -1)56self.image.convert()575859