Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bloch sphere implementation #2

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Bloch sphere implementation #2

wants to merge 4 commits into from

Conversation

niccololaurora
Copy link
Collaborator

@niccololaurora niccololaurora commented Feb 28, 2024

I added a visualization method which implements a Bloch Sphere. This tool could be useful to plot the final states of a classification task.
Example:

state = np.array([1, 0], dtype="complex")
b_sphere = Bloch()
b_sphere.add_vector(state, mode="vector", color="black")
b_sphere.save_plot("b_sphere.pdf")
b_sphere.plot()

.DS_Store Outdated Show resolved Hide resolved
@MatteoRobbiati MatteoRobbiati self-requested a review March 5, 2024 17:48
Copy link
Contributor

@MatteoRobbiati MatteoRobbiati left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for this @niccololaurora!
Some suggestions follow.



class Bloch():
def __init__(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be more natural to use it as follows:

state = circuit.execute().state()
bs = Bloch(state=state)
bs.plot(save=True) 

Then one can just update the cached state as bs.set_state(state=new_state).
I think this would be lighter and intuitive.

Copy link
Collaborator Author

@niccololaurora niccololaurora Mar 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you Matteo for the suggestions.

I may be mistaken, but I don't think the concept of a "cached state" would be useful in this context. Based on my experience, I often find myself needing to plot multiple states. Therefore, a natural approach is to create a Bloch object and then add states to it.

state = np.array([1, 0])
bs = Bloch()
bs.add_state(state)

I will implement the option to pass both a single state (already possible) and a list of states to the add_state function.
Additionally, I have introduced the options of "color" and "mode":

  • Color: In classification tasks, it may be necessary to plot final states associated with different classes, with each class represented by a different color.
  • Mode: "vector" or "point". When plotting many states, a point on the sphere is often clearer and cleaner than a cluster of arrows.

Here I have an example:
sfera_a_q1_l1.pdf]

As evident from the uploaded image, it is challenging to distinguish points at the front and back of the sphere. Hence, I plan to implement a method to blur the color of points on the sphere based on their distance from the observer's perspective. Additionally, another approach could involve saving the image as an .html file, enabling users to interact with the sphere by rotating it to view it from different angles.
Implementing both options is also feasible.

plt.show()


def label_states(self, nclasses):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function should be very problem dependent, right?
If your idea is to encode a 3d polygon inside the sphere, then I would suggest to just define a method bs.distribute_labels_on_surface or bs.inscribed_solid(n_vertexes=10). This will be very useful to show the inscribed solid and also, if needed, to return the label states corresponding to the vertexes.

Copy link
Collaborator Author

@niccololaurora niccololaurora Mar 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function label_states (maybe it could be renamed to something more intuitive) plots the fidelity label states on the Sphere.
I added a flag which if is True, it plots also the 3d polygon, which is supported when there are at least 4 classes

nclasses = 4
bs = Bloch()
label_states = bs.label_states(nclasses, solid=False)

bloch_sphere_4.pdf
bloch_sphere_N4.pdf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants