<jupyter_start><jupyter_text><jupyter_code>import numpy as np
import pandas as pd
import math as m

from qutip import Bloch, basis, Qobj

from qiskit import *
from qiskit.quantum_info import Statevector
color_palette = [
    '#0039A6',
    '#EE352E',
    '#6CBE45',
    '#996633',
    '#A7A9AC',
    '#FCCC0A',
    '#808183',
    '#FF6319',
    '#00933C',
    '#B933AD'
]
# cartesian to spherical coordinate

def cart2sph(x,y,z):
    r = m.sqrt(x**2 + y**2 + z**2)
    theta = m.acos(z/r)
    phi = m.atan2(y,x)
    return r, theta, phi
coor = [0,0,-1]
print("r, theta, phi =", cart2sph(*coor))
# |0> statevector
sv_0 = Statevector.from_label('0')

# rotation circuit
def rotation(theta, phi):
    qc = QuantumCircuit(1)

    # theta rotation
    qc.ry(theta, 0)
    # phi rotation
    qc.rz(phi, 0)

    return qc
b = Bloch()

b.vector_color = color_palette

b.add_states(Qobj(np.array([1,0])), kind='vector')
b.add_states(Qobj(np.array([0,1])), kind='vector')

b.show()
qc = rotation(np.pi/2, np.pi/2)
new_sv = sv_0.evolve(qc)
state = Qobj(new_sv.data)
b = Bloch()

b.add_states(state, kind='point')
b.add_states(state, kind='vector')

b.show()<jupyter_output><empty_output><jupyter_text>Triangle (3 classes)<jupyter_code># cartesian coordinate of triangle vertices (inside a unit sphere)

tri_vecs = [
    [0, 0, 1],
    [np.sqrt(3)/2, 0, -1/2],
    [-np.sqrt(3)/2, 0, -1/2],
]
# plot the cartesian coordinate

b_tri = Bloch()
b_tri.vector_color = color_palette

for i in range(len(tri_vecs)):
    b_tri.add_annotation(tri_vecs[i], str(i))
    b_tri.add_vectors(tri_vecs[i])
    
b_tri.show()
# transform the cartesian coordinate to spherical coordinate

print("Spherical Coordinate")
print("r, theta, phi")
tri_angles = []
for i in range(len(tri_vecs)):
    tri_angles += [cart2sph(*tri_vecs[i])]
    print(str(i+1) + ":", tri_angles[-1])
    
print("Total vertices =", len(tri_angles))
# construct the quantum states for all vertices

tri_states = np.zeros((3,2), dtype=np.complex_)
b_tri_states = Bloch()
b_tri_states.vector_color = color_palette

for i in range(len(tri_angles)):
    # construct quantum circuit to prepare the vertice's quantum state based on its spherical coordinate
    qc = rotation(tri_angles[i][1], tri_angles[i][2])
    # obtain the quantum state (statevector) by applying the circuit to |0>
    new_sv = sv_0.evolve(qc)
    
    # plot the quantum state on a Bloch Sphere
    state = Qobj(new_sv.data)
    b_tri_states.add_states(state, kind='vector')
    # also plot the annotation based on the cartesian coordinate of the vertices as a sanity check
    b_tri_states.add_annotation(tri_vecs[i], "$\Psi_{" + str(i) + "}$")
    
    tri_states[i, :] = new_sv.data
    
b_tri_states.show()
# quantum states of the vertices

# saving the numpy array to txt
np.savetxt("./tri_states.txt", tri_states)
print("Quantum States")
pd.DataFrame(tri_states)
# construct the true label for each class (vertice) from the quantum states

tri_class_label = np.zeros((3,3))
for i in range(3):
    temp = np.zeros((3,))
    for j in range(3):
        temp[j] = (abs(tri_states[i].conjugate()@(tri_states[j].T)))**2
    
    tri_class_label[i, :] = temp

# saving the numpy array to txt
np.savetxt("./tri_class_label.txt", tri_class_label)
print("Class True Labels")
pd.DataFrame(tri_class_label)<jupyter_output>Class True Labels<jupyter_text>Tetrahedron (4 classes)All combination of $(v_{i},v_{j})$ where $i \neq j$ will result in $|\langle v_{i}|v_{j}\rangle|^2 = (\frac{1}{3})^2 = \frac{1}{9}$<jupyter_code># cartesian coordinate of tetrahedron vertices (inside a unit sphere)

tetra_vecs = [
    [0, 0, 1],
    [np.sqrt(8/9), 0, -1/3],
    [-np.sqrt(2/9), np.sqrt(2/3), -1/3],
    [-np.sqrt(2/9), -np.sqrt(2/3), -1/3],
]
# plot the cartesian coordinate

b_tetra = Bloch()
b_tetra.vector_color = color_palette

for i in range(len(tetra_vecs)):
    b_tetra.add_annotation(tetra_vecs[i], str(i))
    b_tetra.add_vectors(tetra_vecs[i])
    
b_tetra.show()
# transform the cartesian coordinate to spherical coordinate

print("Spherical Coordinate")
print("r, theta, phi")
tetra_angles = []
for i in range(len(tetra_vecs)):
    tetra_angles += [cart2sph(*tetra_vecs[i])]
    print(str(i+1) + ":", tetra_angles[-1])
    
print("Total vertices =", len(tetra_angles))
# construct the quantum states for all vertices

tetra_states = np.zeros((4,2), dtype=np.complex_)
b_tetra_states = Bloch()
b_tetra_states.vector_color = color_palette

for i in range(len(tetra_angles)):
    # construct quantum circuit to prepare the vertice's quantum state based on its spherical coordinate
    qc = rotation(tetra_angles[i][1], tetra_angles[i][2])
    # obtain the quantum state (statevector) by applying the circuit to |0>
    new_sv = sv_0.evolve(qc)
    
    # plot the quantum state on a Bloch Sphere
    state = Qobj(new_sv.data)
    b_tetra_states.add_states(state, kind='vector')
    # also plot the annotation based on the cartesian coordinate of the vertices as a sanity check
    b_tetra_states.add_annotation(tetra_vecs[i], "$\Psi_{" + str(i) + "}$")
    
    tetra_states[i, :] = new_sv.data
    
b_tetra_states.show()
# quantum states of the vertices

# saving the numpy array to txt
np.savetxt("./tetra_states.txt", tetra_states)
print("Quantum States")
pd.DataFrame(tetra_states)
# construct the true label for each class (vertice) from the quantum states

tetra_class_label = np.zeros((4,4))
for i in range(4):
    temp = np.zeros((4,))
    for j in range(4):
        temp[j] = (abs(tetra_states[i].conjugate()@(tetra_states[j].T)))**2
    
    tetra_class_label[i, :] = temp

# saving the numpy array to txt
np.savetxt("./tetra_class_label.txt", tetra_class_label)
print("Class True Labels")
pd.DataFrame(tetra_class_label)<jupyter_output>Class True Labels<jupyter_text>Square (8 classes)Similar to the Elongated Square Bipyramid (see below), but only the 8 Square Vertices, A = ($\pm \frac{L}{2}, \pm \frac{L}{2}, \pm \frac{L}{2}$), with $\frac{L}{2} = \frac{1}{\sqrt{3}}$.<jupyter_code># cartesian coordinate of Square vertices (inside a unit sphere)

L = 2/np.sqrt(3)
square_vecs = [
    [L/2, L/2, L/2],
    [L/2, L/2, -L/2],
    [L/2, -L/2, L/2],
    [L/2, -L/2, -L/2],
    [-L/2, L/2, L/2],
    [-L/2, L/2, -L/2],
    [-L/2, -L/2, L/2],
    [-L/2, -L/2, -L/2]
]
# plot the cartesian coordinate

b_square = Bloch()
b_square.vector_color = color_palette

for i in range(len(square_vecs)):
    b_square.add_annotation(square_vecs[i], str(i))
    b_square.add_vectors(square_vecs[i])
    
b_square.show()
# transform the cartesian coordinate to spherical coordinate

print("Spherical Coordinate")
print("r, theta, phi")
square_angles = []
for i in range(len(square_vecs)):
    square_angles += [cart2sph(*square_vecs[i])]
    print(str(i+1) + ":", square_angles[-1])
    
print("Total vertices =", len(square_angles))
# construct the quantum states for all vertices

square_states = np.zeros((8,2), dtype=np.complex_)
b_square_states = Bloch()
b_square_states.vector_color = color_palette

for i in range(len(square_angles)):
    # construct quantum circuit to prepare the vertice's state based on its spherical coordinate
    qc = rotation(square_angles[i][1], square_angles[i][2])
    # obtain the quantum state (statevector) by applying the circuit to |0>
    new_sv = sv_0.evolve(qc)
    
    # plot the quantum state on a Bloch Sphere
    state = Qobj(new_sv.data)
    b_square_states.add_states(state, kind='vector')
    # also plot the annotation based on the cartesian coordinate of the vertices as a sanity check
    b_square_states.add_annotation(square_vecs[i], str(i))
    
    square_states[i, :] = new_sv.data
    
b_square_states.show()
# quantum states of the vertices

# saving the numpy array to txt
np.savetxt("./square_states.txt", square_states)
print("Quantum States")
pd.DataFrame(square_states)
# construct the true label for each class (vertice) from the quantum states

square_class_label = np.zeros((8,8), dtype=np.complex_)
for i in range(8):
    temp = np.zeros((8,))
    for j in range(8):
        temp[j] = (abs(square_states[i].conjugate()@(square_states[j].T)))**2
    
    square_class_label[i, :] = temp

# saving the numpy array to txt
np.savetxt("./square_class_label.txt", square_class_label)
print("Class True Labels")
pd.DataFrame(square_class_label)<jupyter_output>Class True Labels<jupyter_text>Elongated Square Bipyramid (10 classes)There are 3 types of vertices: (the coordinate is written in the form of $(x, y, z)$ inside a unit sphere)1. 8 Square Vertices, A = ($\pm \frac{L}{2}, \pm \frac{L}{2}, \pm \frac{L}{2}$)2. Top Pyramid Vertices, B = (0, 0, 1)3. Bottom Pyramid Vertices, C = (0, 0, -1)with $\frac{L}{2} = \frac{1}{\sqrt{3}}$.There are several combinations:1. $|\langle B|C\rangle|^2 = 1$2. $|\langle B|A\rangle|^2 = |\langle C|A\rangle|^2 = \frac{1}{3}$3. Several combinations of $|\langle A|A\rangle|^2$<jupyter_code># cartesian coordinate of ESB vertices (inside a unit sphere)

L = 2/np.sqrt(3)
ESB_vecs = [
    [0, 0, 1],
    [0, 0, -1],
    [L/2, L/2, L/2],
    [L/2, L/2, -L/2],
    [L/2, -L/2, L/2],
    [L/2, -L/2, -L/2],
    [-L/2, L/2, L/2],
    [-L/2, L/2, -L/2],
    [-L/2, -L/2, L/2],
    [-L/2, -L/2, -L/2]
]
# plot the cartesian coordinate

b_ESB = Bloch()
b_ESB.vector_color = color_palette

for i in range(len(ESB_vecs)):
    b_ESB.add_annotation(ESB_vecs[i], str(i))
    b_ESB.add_vectors(ESB_vecs[i])
    
b_ESB.show()
# transform the cartesian coordinate to spherical coordinate

print("Spherical Coordinate")
print("r, theta, phi")
ESB_angles = []
for i in range(len(ESB_vecs)):
    ESB_angles += [cart2sph(*ESB_vecs[i])]
    print(str(i+1) + ":", ESB_angles[-1])
    
print("Total vertices =", len(ESB_angles))
# construct the quantum states for all vertices

ESB_states = np.zeros((10,2), dtype=np.complex_)
b_ESB_states = Bloch()
b_ESB_states.vector_color = color_palette

for i in range(len(ESB_angles)):
    # construct quantum circuit to prepare the vertice's state based on its spherical coordinate
    qc = rotation(ESB_angles[i][1], ESB_angles[i][2])
    # obtain the quantum state (statevector) by applying the circuit to |0>
    new_sv = sv_0.evolve(qc)
    
    # plot the quantum state on a Bloch Sphere
    state = Qobj(new_sv.data)
    b_ESB_states.add_states(state, kind='vector')
    # also plot the annotation based on the cartesian coordinate of the vertices as a sanity check
    b_ESB_states.add_annotation(ESB_vecs[i], str(i))
    
    ESB_states[i, :] = new_sv.data
    
b_ESB_states.show()
# quantum states of the vertices

# saving the numpy array to txt
np.savetxt("./ESB_states.txt", ESB_states)
print("Quantum States")
pd.DataFrame(ESB_states)
# construct the true label for each class (vertice) from the quantum states

ESB_class_label = np.zeros((10,10), dtype=np.complex_)
for i in range(10):
    temp = np.zeros((10,))
    for j in range(10):
        temp[j] = (abs(ESB_states[i].conjugate()@(ESB_states[j].T)))**2
    
    ESB_class_label[i, :] = temp

# saving the numpy array to txt
np.savetxt("./ESB_class_label.txt", ESB_class_label)
print("Class True Labels")
pd.DataFrame(ESB_class_label)<jupyter_output>Class True Labels
{'fraction_non_alphanumeric': 0.14922428481750516, 'fraction_numerical': 0.02349565061429468, 'mean_word_length': 4.139170506912443, 'pattern_counts': {'":': 4, '<': 17, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '350612', 'license': 'MIT', 'n_tokens_mistral': 4398, 'n_tokens_neox': 4188, 'n_words': 1050, 'path': 'Tools/.ipynb_checkpoints/Geometric Class Hermitian Observable Calculation-checkpoint.ipynb', 'repo_name': 'Graciaira/quantum_image_classifier', 'size': '11151'}