-
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
Changes from 18 commits
c172441
8d3e170
487d2d7
b0994c7
6adb48b
d1aeffb
95095d9
1bbf5b7
ac2ee27
8aa8904
2fb598f
5416f70
6a1f792
f229974
08f109a
202353e
acc1b0d
c541c6b
312273f
b94ac8d
445489c
d2224cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
--- | ||
features: | ||
- | | ||
Some restrictions when using ``wire_order`` in the circuit drawers had been relaxed. | ||
Now, ``wire_order`` can list just qubits and, in that case, it can be used | ||
with ``cregbundle=True``, since it will not affect the classical bits. | ||
|
||
.. 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. oops... fixed in d2224cd |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
\documentclass[border=2px]{standalone} | ||
|
||
\usepackage[braket, qm]{qcircuit} | ||
\usepackage{graphicx} | ||
|
||
\begin{document} | ||
\scalebox{1.0}{ | ||
\Qcircuit @C=1.0em @R=0.2em @!R { \\ | ||
\nghost{{q}_{2} : } & \lstick{{q}_{2} : } & \qw & \qw & \qw & \qw\\ | ||
\nghost{{q}_{1} : } & \lstick{{q}_{1} : } & \gate{\mathrm{X}} & \qw & \qw & \qw\\ | ||
\nghost{{q}_{3} : } & \lstick{{q}_{3} : } & \gate{\mathrm{H}} & \gate{\mathrm{X}} & \qw & \qw\\ | ||
\nghost{{q}_{0} : } & \lstick{{q}_{0} : } & \gate{\mathrm{H}} & \qw & \qw & \qw\\ | ||
\nghost{{c}_{0} : } & \lstick{{c}_{0} : } & \cw & \controlo \cw \cwx[-2] & \cw & \cw\\ | ||
\nghost{{c}_{1} : } & \lstick{{c}_{1} : } & \cw & \controlo \cw \cwx[-1] & \cw & \cw\\ | ||
\nghost{{c}_{2} : } & \lstick{{c}_{2} : } & \cw & \control \cw \cwx[-1] & \cw & \cw\\ | ||
\nghost{{c}_{3} : } & \lstick{{c}_{3} : } & \cw & \control \cw^(0.0){^{\mathtt{0xc}}} \cwx[-1] & \cw & \cw\\ | ||
\nghost{{ca}_{0} : } & \lstick{{ca}_{0} : } & \cw & \cw & \cw & \cw\\ | ||
\nghost{{ca}_{1} : } & \lstick{{ca}_{1} : } & \cw & \cw & \cw & \cw\\ | ||
\\ }} | ||
\end{document} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -681,7 +681,7 @@ def test_idle_wires_barrier(self): | |
|
||
def test_wire_order(self): | ||
"""Test the wire_order option to latex drawer""" | ||
filename = self._get_resource_path("test_latex_wire_order.tex") | ||
filename = self._get_resource_path("test_latex_wire_order_only.tex") | ||
qr = QuantumRegister(4, "q") | ||
cr = ClassicalRegister(4, "c") | ||
cr2 = ClassicalRegister(2, "ca") | ||
|
@@ -699,6 +699,26 @@ def test_wire_order(self): | |
) | ||
self.assertEqualToReference(filename) | ||
|
||
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 commentThe reason will be displayed to describe this comment to others. Learn more. This test says its There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main(verbosity=2) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -341,6 +341,37 @@ def test_wire_order(self): | |
str(_text_circuit_drawer(circuit, wire_order=[2, 1, 3, 0, 6, 8, 9, 5, 4, 7])), expected | ||
) | ||
|
||
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 commentThe reason will be displayed to describe this comment to others. Learn more. This test says its There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. test removed. See #9893 (comment) |
||
|
||
def test_text_swap(self): | ||
"""Swap drawing.""" | ||
expected = "\n".join( | ||
|
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