-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Relax wire_order restrictions in circuit visualization #9893
Relax wire_order restrictions in circuit visualization #9893
Conversation
Thank you for opening a new pull request. Before your PR can be merged it will first need to pass continuous integration tests and be reviewed. Sometimes the review process can be slow, so please be patient. While you're waiting, please feel free to review other open PRs. While only a subset of people are authorized to approve pull requests for merging, everyone is encouraged to review open pull requests. Doing reviews helps reduce the burden on the core team and helps make the project's code better for everyone. One or more of the the following people are requested to review this:
|
qiskit/opflow/__init__.py
Outdated
@@ -19,7 +19,7 @@ | |||
|
|||
Operators and State functions are the building blocks of Quantum Algorithms. | |||
|
|||
A library for Quantum Algorithms & Applications is more than a collection of | |||
A library for Quantum Algorithms & Applications (QA&A) is more than a collection of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is unrelated. I just saw it around and noticed that QA&A is not a very common acronym.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'd probably be better to revert this, especially with opflow deprecated. It's a problem that's existed forever (and capitalising unexpected words in a sentence like this is a not-altogether-unheard-of way of implicitly defining an acronym anyway).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done in b94ac8d
Pull Request Test Coverage Report for Build 5542464288
💛 - Coveralls |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks great thanks for fixing this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is a good idea. Allowing only a partial wire_map
I think is very confusing behaviour, and not generally clear like the initial intent of the option - I personally would have expected that giving a partial wire_map
would only draw those wires, if it was going to have non-error behaviour. Really though, it just looks like an error to me.
If you really want this behaviuor for wire_map
, can we at least make the syntax [2, 3, ...]
(i.e. with a literal Ellipsis
)?
To the contrary (and probably because I'm too famiiar with the drawer), only drawing some wires makes little sense to me. (how do you express a I saw some numpy |
We spoke about this in the dev meeting just now. The notes are:
Given this, I'd vote to go with option 1: the |
Oh, and about "how do you draw |
…_order_restrictions
…_order_restrictions
done in that way. Have a look. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new logic looks good, thanks, and consistent with what I remember us discussing. Could you fix the tests so they test what they say they do?
.. code-block:: | ||
|
||
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister | ||
|
||
qr = QuantumRegister(4, "q") | ||
cr = ClassicalRegister(4, "c") | ||
cr2 = ClassicalRegister(2, "ca") | ||
circuit = QuantumCircuit(qr, cr, cr2) | ||
circuit.h(0) | ||
circuit.h(3) | ||
circuit.x(1) | ||
circuit.x(3).c_if(cr, 10) | ||
circuit.draw('text', wire_order=[2, 3], cregbundle=True) | ||
|
||
.. parsed-literal:: | ||
|
||
q_2: ──────────── | ||
┌───┐ ┌───┐ | ||
q_3: ┤ H ├─┤ X ├─ | ||
├───┤ └─╥─┘ | ||
q_0: ┤ H ├───╫─── | ||
├───┤ ║ | ||
q_1: ┤ X ├───╫─── | ||
└───┘┌──╨──┐ | ||
c: 4/═════╡ 0xa ╞ | ||
└─────┘ | ||
ca: 2/════════════ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this example will work after the new changes any more because of the partial wire order. We should replace it with one that assigns all qubits.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops... fixed in d2224cd
def test_wire_order_only_qubits(self): | ||
"""Test the wire_order option with only qubits""" | ||
expected = "\n".join( | ||
[ | ||
" ", | ||
"q_2: |0>────────────", | ||
" ┌───┐ ", | ||
"q_1: |0>┤ X ├───────", | ||
" ├───┤ ┌───┐ ", | ||
"q_3: |0>┤ H ├─┤ X ├─", | ||
" ├───┤ └─╥─┘ ", | ||
"q_0: |0>┤ H ├───╫───", | ||
" └───┘┌──╨──┐", | ||
" c: 0 4/═════╡ 0xa ╞", | ||
" └─────┘", | ||
"ca: 0 2/════════════", | ||
" ", | ||
] | ||
) | ||
qr = QuantumRegister(4, "q") | ||
cr = ClassicalRegister(4, "c") | ||
cr2 = ClassicalRegister(2, "ca") | ||
circuit = QuantumCircuit(qr, cr, cr2) | ||
circuit.h(0) | ||
circuit.h(3) | ||
circuit.x(1) | ||
circuit.x(3).c_if(cr, 10) | ||
self.assertEqual( | ||
str(_text_circuit_drawer(circuit, wire_order=[2, 1, 3, 0, 4, 5, 6, 7, 8, 9])), expected | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test says its wire_order
is only qubits, but it actually has all bits.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
test removed. See #9893 (comment)
def test_wire_order_only_qubits(self): | ||
"""Test the wire_order list with qubits only to latex drawer""" | ||
filename = self._get_resource_path("test_wire_order_only_qubits.tex") | ||
qr = QuantumRegister(4, "q") | ||
cr = ClassicalRegister(4, "c") | ||
cr2 = ClassicalRegister(2, "ca") | ||
circuit = QuantumCircuit(qr, cr, cr2) | ||
circuit.h(0) | ||
circuit.h(3) | ||
circuit.x(1) | ||
circuit.x(3).c_if(cr, 12) | ||
circuit_drawer( | ||
circuit, | ||
cregbundle=False, | ||
wire_order=[2, 1, 3, 0, 4, 5, 6, 7, 8, 9], | ||
filename=filename, | ||
output="latex_source", | ||
) | ||
self.assertEqualToReference(filename) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test says its wire_order
is only qubits, but it's actually got all of them. It should probably be a test of what it says, and shouldn't include the clbits.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually, these tests make little sense now. Removing them in 445489c and add those that cover the involved code in this PR.
qiskit/opflow/__init__.py
Outdated
@@ -19,7 +19,7 @@ | |||
|
|||
Operators and State functions are the building blocks of Quantum Algorithms. | |||
|
|||
A library for Quantum Algorithms & Applications is more than a collection of | |||
A library for Quantum Algorithms & Applications (QA&A) is more than a collection of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'd probably be better to revert this, especially with opflow deprecated. It's a problem that's existed forever (and capitalising unexpected words in a sentence like this is a not-altogether-unheard-of way of implicitly defining an acronym anyway).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks much better now, thanks. I think the API we've settled on is the right compromise between permissibility and not being surprising.
Just a note for all concerned, when we use |
Requested by @ajavadia
Drawing circuits with
wire_order
has some restrictions, coming from the original design that, I think, can be relaxed a bit. For example ifwire_order
only refers to qubits, thencregbundle
can be preserver. So this PR relax those restrictions and allows you to writewire_order=[2, 3, 0, 1]
andcregbundle=True
in a 4 qubit circuit, for example: