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

Adding a new argument to plot_state_qsphere to visualize all the possible states on qsphere when set to True #5607

Closed
wants to merge 9 commits into from

Conversation

pragyakatyayan
Copy link

@pragyakatyayan pragyakatyayan commented Jan 11, 2021

*** This PR is being carried forward to #6596 with resolved conflicts, no test failures and updated code of state_visualization.py ***

UPDATE #1:

I tried to do a PR (#5177) on 3 October 2020, but it was failing some tests. Also visualization code has changed with new releases. So, this time I am trying afresh after performing all local tests. Hope it proves helpful to the community.

Motivation

I ran into this situation where only some states were visible while visualizing the qsphere few months back and asked help here. It was then that @aasfaw motivated me to do something like this. Many thanks to him. See the conversation here.

Summary

The previous show_state_labels argument is meant to show labels for only weighted states. When used with show_state_phases=True the weighted states displayed their phase values. The states with 0 weights were not given the labels.

The new 'optional' argument show_all_state_labels which is added with this PR shows all possible state labels when set to True. This Boolean variable which is false by default, aims at letting the end-user distinguish visually on the q-sphere between the states having zero weights and those having non-zero weights. The argument is kept optional to ensure that it is set to true only if the user wants to display all possible state labels for any particular circuit, the default view would show labels (and phases) for non-zero states only.
The new argument works well with show_state_phases=True and shows phase values for all possible state labels.
The test cases given below are performed for two circuits-one with 2 qubits and other with 3 qubits. Both the circuits were deliberately made to produce weights for half of the possible states, to notice the working of new argument for states with zero-weights. Snapshots show the default view and the new argument's output.

Details and comments

Case 1: Tested for two-qubit circuit (clutter possibility is low)

Default:
image

New Argument: show_all_state_labels=True
image

with show_state_phases=True

Default:
image

New Argument: show_all_state_labels=True, show_state_phases=True
image


Case 2: Tested for three-qubit circuit (clutter possibility is medium-high)

Default:
image

New Argument:
image

with show_state_phases=True

Default:
image

New Argument:
image


CAUTION: Users must note that this new argument might give you a cluttered output in case of qubits>3. That is why the argument's default setting is False. This argument must be set to True if and only if user wants to see the positioning of all possible states on the qsphere.

Copy link
Member

@mtreinish mtreinish left a comment

Choose a reason for hiding this comment

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

Thanks for pushing a PR, I took a quick look and personally I tend to agree with @aasfaw that I feel this ends up making a more cluttered visualization. But, it's fine to add a flag to make this an opt-in view for people who find it useful. But, before this is ready I think we need to provide some more detail in the docs about the potential limitations with the flag, especially with more than a couple qubits. I also think there is a bug in showing the state phase for a label.

Comment on lines 666 to 667
show_all_state_labels (bool): An optional boolean indicating whether to
show labels for all basis states.
Copy link
Member

Choose a reason for hiding this comment

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

I think we probably need to provide more guidance on this option to the documentation here. This flag really isn't desirable with more than a few qubits. For example I just passed in a 5 qubit random state to the drawer and set this flag and it returned:

test_nophase

which is too cluttered. We probably should mention that this defaults false, and that with larger qubit counts (maybe >3 or something like that) the number of state labels can clutter the output.

Copy link
Author

@pragyakatyayan pragyakatyayan Jan 15, 2021

Choose a reason for hiding this comment

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

Thanks @mtreinish for reviewing this PR. Yes, you are absolutely right, this argument would clutter the image for more than 3 qubits. That is why I marked it 'False' by default. Only motive of introducing this new argument is to let the interested user know visually where on the qsphere are all the states located and which ones out of all possible states have magnitude. 2D view is a bit messy but may be a 3D view would carry this argument well without clutter.

I will make the changes in documentation as per your suggestion, so that the user is well aware of what he might encounter.

Comment on lines 819 to 825
if show_state_phases:
element_angle = (np.angle(state[i]) + (np.pi * 4)) % (np.pi * 2)
if use_degrees:
element_text += '\n$%.1f^\\circ$' % (element_angle * 180/np.pi)
else:
element_angle = pi_check(element_angle, ndigits=3).replace('pi', '\\pi')
element_text += '\n$%s$' % (element_angle)
Copy link
Member

Choose a reason for hiding this comment

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

I think there is an issue here. When setting both show_state_phases and show_all_state_labels it looks
like the phases are writing over itself:

test

Copy link
Author

@pragyakatyayan pragyakatyayan Jan 15, 2021

Choose a reason for hiding this comment

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

I wasn't able to replicate this bug, could you please help me understand when you got this? I tried a random 5-qubit circuit with both arguments set to true, and got the following:
image

This is cluttered for sure but does not look over written like yours. If I could understand what gave you that bug, I will try to resolve it too within this PR. Thanks.

pragyakatyayan added a commit to pragyakatyayan/qiskit-terra that referenced this pull request Jun 17, 2021
@HuangJunye HuangJunye added the Community PR PRs from contributors that are not 'members' of the Qiskit repo label Jun 21, 2022
@prakharb10
Copy link
Contributor

Superseded by #6596.

@jakelishman
Copy link
Member

Closing as superseded.

@jakelishman jakelishman closed this Mar 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Community PR PRs from contributors that are not 'members' of the Qiskit repo
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

5 participants