Qiskit / qiskit

Qiskit is an open-source SDK for working with quantum computers at the level of extended quantum circuits, operators, and primitives.
https://www.ibm.com/quantum/qiskit
Apache License 2.0
5.15k stars 2.35k forks source link

Get Hadamard Basis as output in the draw function? #8014

Open rishacha opened 2 years ago

rishacha commented 2 years ago

What should we add?

Is it possible to visualize the latex state in the Hadamard Basis instead of the normal Computational Basis. In the state_vector.draw(output='latex'), can a basis param be added to visualize it in Hadamard basis instead of computational.

Please excuse if this is already implemented. Is there already a workaround present for this? I want to deal with Dirac notation and the draw function really helps. I don't want to get into the Bloch sphere part. I just need my bra-kets to contain +/-

jakelishman commented 2 years ago

We don't currently support this, and there isn't a particular workaround as things stand because the drawers all just use the internal memory of the statevector, which is stored in the computational basis, and generate the ket labels for the indices of each data element.

I'm somewhat hesitant about this feature, because it would be quite specific to this one drawer and might have a messy interface if we try to be too general, but I think if the feature is kept very small in scope, it could be ok to include.

The approximate plan would be to make qiskit.visualization.state_visualization.state_to_latex take an extra keyword argument ket_basis that defaults to z, but can take hadamard (or call it x or y if having it in terms of y eigenstates would be useful). It'd pass those down to whatever functions also needed them, including ket_name. To actually do the mapping, probably we'd just apply h (or Z + iY or whatever it shakes out to) on all the qubits as appropriate, and then pass the new array down to the drawers.

This could be good for an external contributor looking for a little more than a bugfix.

rishacha commented 2 years ago

I want to help implement this but I don't know where to start. Can I take some time and try this out from my side? I'll take a look at the repo and the CONTRIBUTING.md to start off.

jakelishman commented 2 years ago

Yeah, absolutely - I'll assign you so others know you're working on it, but don't feel any time pressure. Please ask questions if you have them, and one of the community team or I will help out.

rishacha commented 2 years ago

I am looking at this particular function.

The basic idea is to modify the **drawer_args to include a ket_basis argument for this particular drawer. It's always an np.log2 size for List[complex]

I can therefore modify a copy of the data variable to change the indices and the data to get the right ket basis

For example -

$$ \begin{equation} \begin{bmatrix} \alpha \\ \beta \\ \gamma \\ \theta \end{bmatrix} = \frac{1}{2} \begin{bmatrix} (\alpha + \beta + \gamma + \theta) \\ (\alpha - \beta - \gamma + \theta) \\ (\alpha + \beta - \gamma - \theta) \\ (\alpha - \beta + \gamma - \theta) \end{bmatrix}\end{equation} $$ would mean applying an H gate. Since the data stored is in the state_vector's data variable is always in the computational basis, does this logic make sense?

I have a few questions -

  1. Can I add more comments on my feat branch to understand it a bit better?
  2. How is latex managed on Github issues? It seems like an open issue

I plan on changing this particular function -

https://github.com/Qiskit/qiskit-terra/blob/0041b6ff262c7132ee752b8e64826de1167f690f/qiskit/visualization/state_visualization.py#L1238-L1262

rishacha commented 2 years ago

@jakelishman - Is there someone who can help me out with this?

jakelishman commented 2 years ago

Sorry about that, I missed the comment somehow.

$$ \begin{pmatrix} \alpha\ \beta \end{pmatrix} \to \frac1{\sqrt2} \begin{pmatrix} \alpha + \beta \ \alpha - \beta \end{pmatrix} $$

(though just trying it then, I will say its whitespace handling leaves something to be desired...)

Thanks for tagging me - feel free to do that if I fail to respond again.

rishacha commented 2 years ago

@jakelishman - Any idea on how to get this fix in place?

Command to reproduce - pip install .

Seems like an issue in setup.py

   File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 792, in resolve
          raise VersionConflict(dist, req).with_context(dependent_req)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      pkg_resources.VersionConflict: (setuptools 45.2.0 (/usr/lib/python3/dist-packages), Requirement.parse('setuptools>=58.0'))

Attached log_file - qiskit.log

rishacha commented 2 years ago

@jakelishman - I fixed the above. Please ignore

jakelishman commented 2 years ago

Ah glad to hear it! I'm guessing it was to do with your Python install being from your package manager, and some of the core machinery (setuptools, pkg_resources and things like that) being a bit out of date.

Just in case you didn't know: we usually suggest working in virtual environments, so you can install everything you need without affecting your system's Python installation. We have a little bit of documentation on setting up virtual environments if you need it.