Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

📚 The CoCalc Library - books, templates and other resources

132930 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
import numpy as np
19
from qiskit import BasicAer, execute
20
from qiskit.aqua.translators.ising import max_cut
21
from vqe_playground.utils.colors import WHITE, BLACK
22
from vqe_playground.utils.fonts import ARIAL_30, ARIAL_36
23
from vqe_playground.utils.labels import graph_node_labels_reversed_str
24
from vqe_playground.utils.states import comp_basis_states, NUM_QUBITS, NUM_STATE_DIMS
25
26
27
class ExpectationGrid(pygame.sprite.Sprite):
28
"""Displays a grid that contains basis states, eigenvalues, and probabilities"""
29
def __init__(self, circuit, adj_matrix):
30
pygame.sprite.Sprite.__init__(self)
31
self.eigenvalues = None
32
self.maxcut_shift = 0
33
self.image = None
34
self.rect = None
35
self.basis_states = comp_basis_states(NUM_QUBITS)
36
self.quantum_state = None
37
self.cur_exp_val = 0
38
self.cur_basis_state_idx = 0
39
self.basis_state_dirty = False
40
41
# When setting circuit this first time,
42
# don't calculate the expectation value
43
# or draw the expectation grid, as the
44
# adjacency matrix hasn't yet been supplied
45
self.set_circuit(circuit, recalc=False)
46
self.set_adj_matrix(adj_matrix)
47
48
# def update(self):
49
# # Nothing yet
50
# a = 1
51
52
def set_circuit(self, circuit, recalc=True):
53
backend_sv_sim = BasicAer.get_backend('statevector_simulator')
54
job_sim = execute(circuit, backend_sv_sim)
55
result_sim = job_sim.result()
56
self.quantum_state = result_sim.get_statevector(circuit, decimals=3)
57
58
if recalc:
59
self.calc_expectation_value()
60
self.draw_expectation_grid()
61
62
def set_adj_matrix(self, adj_matrix):
63
maxcut_op, self.maxcut_shift = max_cut.get_max_cut_qubitops(adj_matrix)
64
# print("maxcut_op: ", maxcut_op, ", maxcut_shift: ", maxcut_shift)
65
66
# TODO: Find different approach of calculating and retrieving diagonal
67
maxcut_op._paulis_to_matrix()
68
self.eigenvalues = maxcut_op._dia_matrix
69
70
self.calc_expectation_value()
71
self.draw_expectation_grid()
72
73
def draw_expectation_grid(self):
74
self.image = pygame.Surface([(NUM_QUBITS + 1) * 50 + 450, 100 + NUM_STATE_DIMS * 50])
75
self.image.convert()
76
self.image.fill(WHITE)
77
self.rect = self.image.get_rect()
78
79
block_size = 26
80
x_offset = 400
81
y_offset = 10
82
83
# Display expectation value and other relevant values
84
text_surface = ARIAL_36.render('Weighted average: ' + str(round(self.cur_exp_val, 2)), False, (0, 0, 0))
85
self.image.blit(text_surface, (0, y_offset + block_size * 13))
86
87
text_surface = ARIAL_36.render('Lowest eigenvalue: ' + str(round(min(self.eigenvalues), 1)), False, (0, 0, 0))
88
self.image.blit(text_surface, (0, y_offset + block_size * 14))
89
90
maxcut_cost = round(self.cur_exp_val - min(self.eigenvalues), 2)
91
text_surface = ARIAL_36.render('Maxcut cost: ' + str(maxcut_cost), False, (0, 0, 0))
92
self.image.blit(text_surface, (0, y_offset + block_size * 15))
93
94
text_surface = ARIAL_36.render('Basis state: ' + str(self.basis_states[self.cur_basis_state_idx]),
95
False, (0, 0, 0))
96
self.image.blit(text_surface, (0, y_offset + block_size * 16))
97
98
text_surface = ARIAL_36.render('Maxcut eigenval shift: ' + str(round(self.maxcut_shift, 1)), False, (0, 0, 0))
99
self.image.blit(text_surface, (0, y_offset + block_size * 17))
100
101
text_surface = ARIAL_36.render('Maxcut weight total: ' + str(round(self.cur_exp_val + self.maxcut_shift, 2)), False, (0, 0, 0))
102
self.image.blit(text_surface, (0, y_offset + block_size * 18))
103
104
# Display column headings
105
node_letter_str = graph_node_labels_reversed_str(NUM_QUBITS)
106
text_surface = ARIAL_30.render(node_letter_str + ' Eigenval Prob', False, (0, 0, 0))
107
self.image.blit(text_surface, (x_offset, y_offset + block_size / 2))
108
109
for y in range(NUM_STATE_DIMS):
110
text_surface = ARIAL_36.render(self.basis_states[y] + ": " + str(round(self.eigenvalues[y], 1)),
111
False, (0, 0, 0))
112
self.image.blit(text_surface, (x_offset, (y + 2) * block_size + y_offset))
113
114
prop_square_side = abs(self.quantum_state[y]) * block_size
115
rect = pygame.Rect(x_offset + 40 - (prop_square_side / 2) + NUM_QUBITS * 30,
116
(y + 1) * block_size + 35 + ((block_size - prop_square_side) / 2),
117
prop_square_side,
118
prop_square_side)
119
if abs(self.quantum_state[y]) > 0:
120
pygame.draw.rect(self.image, BLACK, rect, 2)
121
122
def calc_expectation_value(self):
123
statevector_probs = np.absolute(self.quantum_state) ** 2
124
exp_val = np.sum(self.eigenvalues * statevector_probs)
125
self.cur_exp_val = exp_val
126
127
basis_state_idx = np.argmax(statevector_probs)
128
129
if basis_state_idx != self.cur_basis_state_idx:
130
self.basis_state_dirty = True
131
self.cur_basis_state_idx = basis_state_idx
132
133
# print ("in calc_expectation_value, exp_val: ", exp_val, ", basis state: ", self.basis_states[basis_state_idx])
134
return exp_val, self.basis_states[basis_state_idx]
135
136
137