From 0c95d9491ffeee5c96cb75593048e0a0d5293520 Mon Sep 17 00:00:00 2001 From: 123freezebrennan Date: Thu, 12 Sep 2024 23:56:34 -0700 Subject: [PATCH 1/3] Found issue in qsphere, need to heavily improve rendering in/organization as it is extremely poor n>=10 qubits --- src/visualize/q_sphere.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/visualize/q_sphere.py b/src/visualize/q_sphere.py index 4e7af77..316b887 100644 --- a/src/visualize/q_sphere.py +++ b/src/visualize/q_sphere.py @@ -3,7 +3,7 @@ import numpy as np from matplotlib.cm import ScalarMappable from .base.sphere import sphere -from ..tools import probability, amplitude, phase_angle +from ..tools import probability, amplitude, phaseangle def hamming_distance(l1: str, l2: str): @@ -61,9 +61,9 @@ def q_sphere( show: bool = True, darkmode: bool = True, ): - num_qubits = int(np.log2((len(quantumstate)))) + num_qubits = int(np.log2((len(quantumstate.state)))) probs = probability(quantumstate) - angle = phase_angle(quantumstate) + angle = phaseangle(quantumstate) state_list = [format(i, "b").zfill(num_qubits) for i in range(2**num_qubits)] prob_dict = {state_list[i]: probs[i] for i in range(len(state_list))} phase_dict = {state_list[i]: angle[i] for i in range(len(state_list))} @@ -77,12 +77,10 @@ def q_sphere( _accent = "black" _background = "white" ax = sphere(_background) - coords = get_coords(num_qubits, lat_vals) ham_states = [item for sublist in lat_vals for item in sublist] colors = plt.get_cmap("hsv") norm = plt.Normalize(0, np.pi * 2) - for i, j in zip(coords, ham_states): cur_prob = prob_dict[j] cur_phase = phase_dict[j] @@ -91,8 +89,7 @@ def q_sphere( ax.plot3D(x, y, z, color=colors(norm(cur_phase))) ax.scatter(x[1], y[1], z[1], s=5, color=colors(norm(cur_phase))) ax.text(x[1] * 1.15, y[1] * 1.15, z[1] * 1.15, f"|{j}>", color=_text) - - cbar = plt.colorbar(ScalarMappable(cmap=colors, norm=norm), shrink=0.55) + cbar = plt.colorbar(plt.cm.ScalarMappable(cmap=colors, norm=norm), ax=plt.gca(), shrink=0.55) cbar.set_label("Phase Angle", rotation=270, labelpad=15, color=_accent) cbar.set_ticks([2 * np.pi, (3 * np.pi) / 2, np.pi, np.pi / 2, 0]) cbar.ax.yaxis.set_tick_params(color=_text) From c68635b0c43368d1e2767c578fccad221234d626 Mon Sep 17 00:00:00 2001 From: 123freezebrennan Date: Thu, 12 Sep 2024 23:59:05 -0700 Subject: [PATCH 2/3] Forgot black formatter :) --- src/visualize/q_sphere.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/visualize/q_sphere.py b/src/visualize/q_sphere.py index 316b887..8a0377b 100644 --- a/src/visualize/q_sphere.py +++ b/src/visualize/q_sphere.py @@ -89,7 +89,9 @@ def q_sphere( ax.plot3D(x, y, z, color=colors(norm(cur_phase))) ax.scatter(x[1], y[1], z[1], s=5, color=colors(norm(cur_phase))) ax.text(x[1] * 1.15, y[1] * 1.15, z[1] * 1.15, f"|{j}>", color=_text) - cbar = plt.colorbar(plt.cm.ScalarMappable(cmap=colors, norm=norm), ax=plt.gca(), shrink=0.55) + cbar = plt.colorbar( + plt.cm.ScalarMappable(cmap=colors, norm=norm), ax=plt.gca(), shrink=0.55 + ) cbar.set_label("Phase Angle", rotation=270, labelpad=15, color=_accent) cbar.set_ticks([2 * np.pi, (3 * np.pi) / 2, np.pi, np.pi / 2, 0]) cbar.ax.yaxis.set_tick_params(color=_text) From 6ea1c324926114139cc1f3f7c775ab524eb26df7 Mon Sep 17 00:00:00 2001 From: 123freezebrennan Date: Fri, 13 Sep 2024 00:41:10 -0700 Subject: [PATCH 3/3] Adding other visualizers to confirm they all work --- src/visualize/bloch.py | 2 +- src/visualize/probability.py | 8 +++++--- src/visualize/state_vector.py | 11 +++++++---- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/visualize/bloch.py b/src/visualize/bloch.py index 201d12a..4eaf2a5 100644 --- a/src/visualize/bloch.py +++ b/src/visualize/bloch.py @@ -42,7 +42,7 @@ def bloch( y = 1 * np.sin(theta) * np.sin(phi) z = 1 * np.cos(theta) xs, ys, zs = [0, x], [0, y], [0, z] - ax.plot3D(xs, ys, zs, color=_accent) + ax.plot3D(xs, ys, zs, color=_accent, markevery=100) ax.scatter(xs[1], ys[1], zs[1], s=5, color=_accent) ax.text(xs[1] * 1.15, ys[1] * 1.15, zs[1] * 1.15, "|ψ⟩", color=_text) plt.tight_layout() diff --git a/src/visualize/probability.py b/src/visualize/probability.py index 8837428..d4ce2c8 100644 --- a/src/visualize/probability.py +++ b/src/visualize/probability.py @@ -1,6 +1,7 @@ import matplotlib.pyplot as plt from .base.graph import graph -from ..tools import probability +from ..tools import probability as prob +from ..tools.base import convert_state import numpy as np @@ -11,9 +12,10 @@ def probability( show: bool = True, darkmode: bool = True, ): - num_qubits = np.log2(len(state)) + + num_qubits = int(np.log2(len(convert_state(state)))) state_list = [format(i, "b").zfill(num_qubits) for i in range(2**num_qubits)] - percents = [i * 100 for i in probability(state)] + percents = [i * 100 for i in prob(state)] if darkmode: _text = "white" _accent = "#39c0ba" diff --git a/src/visualize/state_vector.py b/src/visualize/state_vector.py index 72e2376..1451600 100644 --- a/src/visualize/state_vector.py +++ b/src/visualize/state_vector.py @@ -3,7 +3,8 @@ from matplotlib.cm import ScalarMappable from matplotlib.colors import rgb2hex from .base.graph import graph -from ..tools import amplitude, phase_angle +from ..tools import amplitude, phaseangle +from ..tools.base import convert_state def state_vector( @@ -13,10 +14,10 @@ def state_vector( show: bool = False, darkmode: bool = True, ): - num_qubits = int(np.log2(circuit.size)) + num_qubits = int(np.log2(len(convert_state(circuit)))) state_list = [format(i, "b").zfill(num_qubits) for i in range(2**num_qubits)] amplitutes = amplitude(circuit, num_qubits) - phase_angles = phase_angle(circuit, num_qubits) + phase_angles = phaseangle(circuit) if darkmode: _text = "white" _accent = "#39c0ba" @@ -34,7 +35,9 @@ def state_vector( plt.xlabel("Computational basis states", color=_accent) plt.ylabel("Amplitutde", labelpad=5, color=_accent) plt.title("State Vector", pad=10, color=_accent) - cbar = plt.colorbar(ScalarMappable(cmap=colors, norm=norm)) + cbar = plt.colorbar( + plt.cm.ScalarMappable(cmap=colors, norm=norm), ax=plt.gca(), shrink=0.55 + ) cbar.set_label("Phase Angle", rotation=270, labelpad=10, color=_accent) cbar.set_ticks([2 * np.pi, (3 * np.pi) / 2, np.pi, np.pi / 2, 0]) cbar.ax.yaxis.set_tick_params(color=_text)