1
$\begingroup$

Working through the Qiskit text and came across this bit of coding on the density matrix page:

psi_AB.draw('latex', prefix='|\\psi_{AB}\\rangle = ')

which is supposed to produce... $$|\psi_{AB}\rangle = 1/\sqrt{2}(|00\rangle +|11\rangle)$$ However, it seems that the prefix as a part of the .draw() no longer works to create this output so I am attempting to write the the $|\psi_{AB}\rangle = $ portion just as I have done here. The problem is that this is not working in Jupyter notebook and I am not sure what I could be doing wrong.

I've tried single and double $'s.I've tried single and double quotations. I've attempted to use display() and print(). No matter what I try I end up with syntax errors that indicate the dollar signs as the issue or it prints |\psi_{AB}\rangle = without converting to latex. Any help with this coding issue would be appreciated!

Edit: I have run an update on my qiskit and everything now works exactly how it does on the Density matrix page in the qiskit text. They have errors with the prefix in the Statevector and so do I. The page is just to emphasize where the code came from. The real problem is that writing 'DollarSign x=5 DollarSign' in my Jupyter notebook does not change it to the latex $x=5$ as it does here. Instead I get a syntax error indicating the first dollar sign. Perhaps there is something I need to import to make this feature work?

Edit 2.0 Pylatexenc has been properly installed and is up to date. I've figured out how to get the markdown to work when I wish to simply type text. I now feel that the error is in the source coding for qiskit.quantum_info.Statevector.draw. I believe this function is not correctly creating the prefix because the coding, as seen below, does not allow for prefix as an arguement.

operator_shape = state._op_shape
    # we only use the ket convetion for qubit statevectors
    # this means the operator shape should hve no input dimensions and all output dimensions equal to 2
    is_qubit_statevector = len(operator_shape.dims_r()) == 0 and set(operator_shape.dims_l()) == {2}
    if convention == "ket" and is_qubit_statevector:
        latex_str = _state_to_latex_ket(state._data)
    else:
        latex_str = array_to_latex(state._data, source=True, **args)
    return prefix + latex_str + suffix

As seen in the excerpt above the _state_to_latex_ket() does not allow for any arguments other than the state data which would be why it continues to give me errors no matter how I type the prefix in!

If I am correct, then this renders my question moot! Thank you for your help!

$\endgroup$
7
  • $\begingroup$ What version of qiskit are you using? $\endgroup$
    – ryanhill1
    Aug 3 at 14:48
  • $\begingroup$ When I run qiskit.__qiskit_version__ I get...{'qiskit-terra': '0.20.2', 'qiskit-aer': '0.10.4', 'qiskit-ignis': '0.7.1', 'qiskit-ibmq-provider': '0.19.1', 'qiskit-aqua': None, 'qiskit': '0.36.2', 'qiskit-nature': None, 'qiskit-finance': None, 'qiskit-optimization': None, 'qiskit-machine-learning': None} $\endgroup$
    – PGibbon
    Aug 3 at 15:47
  • $\begingroup$ Ok, so the qiskit.__qiskit_version__ used in the DM textbook page you linked is: {'qiskit-terra': '0.21.0', 'qiskit-aer': '0.10.4', 'qiskit-ignis': None, 'qiskit-ibmq-provider': '0.19.2', 'qiskit': '0.37.0', 'qiskit-nature': '0.4.3', 'qiskit-finance': '0.3.3', 'qiskit-optimization': '0.4.0', 'qiskit-machine-learning': '0.4.0'}. Try updating to those versions and see if that fixes it, and if not lmk and I can revisit $\endgroup$
    – ryanhill1
    Aug 3 at 16:39
  • $\begingroup$ @ryalhill1 I have tried your suggestion and it definitely brought me up to speed on qiskit itself. Thank you for your help! However, it did not solve my underlying problem now explained in my edit above. $\endgroup$
    – PGibbon
    Aug 3 at 17:51
  • $\begingroup$ Might be too trivial, but have you tried to print the whole line? Ex: print(psi_AB.draw('latex', prefix='|\\psi_{AB}\\rangle = ')) This works sometimes in other environments like Jupyter, haven't tried it in a Notebook though. $\endgroup$
    – Sam_QC
    Aug 3 at 18:19

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.