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

Add circuit diagrams to source code #7350

Merged
merged 30 commits into from
Feb 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
fc47935
Add circuit diagrams to test_hoare_opt.py
yjt98765 Nov 24, 2021
80e8d57
Add circuit diagram to equivalence_library.py
yjt98765 Nov 26, 2021
361fd9f
Add a TODO in equivalence_library.py
yjt98765 Nov 28, 2021
5ec4e51
Add circuit diagram to echo_rzx_weyl_decomposition
yjt98765 Dec 1, 2021
8c64bed
Merge branch 'Qiskit:main' into diagram
yjt98765 Dec 1, 2021
e55d0f8
Add circuit diagram to merge_adjacent_barriers.py
yjt98765 Dec 1, 2021
a8fd4f3
Add circuit diagram to test/python/circuit/library
yjt98765 Dec 1, 2021
b252a54
Add circuit diagram to test/python/circuit
yjt98765 Dec 2, 2021
30ba283
Add circuit diagram to test/python/compiler
yjt98765 Dec 2, 2021
f7a37d0
Add circuit diagram to test/python/dagcircuit
yjt98765 Dec 2, 2021
07ff304
Add circuit diagram to test/python
yjt98765 Dec 3, 2021
846891c
Merge branch 'Qiskit:main' into diagram
yjt98765 Dec 3, 2021
1843a2d
Format source code with black
yjt98765 Dec 3, 2021
9f1678c
Chang block comment from string to line annotation
yjt98765 Dec 3, 2021
a8f8b96
Merge branch 'main' into diagram
yjt98765 Dec 4, 2021
6bb957d
Add circuit diagram to standard_gates
yjt98765 Dec 9, 2021
b442a50
Add parsed-literal before circuit diagram
yjt98765 Dec 10, 2021
7f4341a
fix a lint problem
yjt98765 Dec 10, 2021
d269a31
Update circuit diagram in test
yjt98765 Dec 10, 2021
76a5c46
Fix a lint problem
yjt98765 Dec 11, 2021
abefa04
Merge branch 'main' into diagram
yjt98765 Dec 11, 2021
8d921bb
Add blank lines before and after parsed-literal
yjt98765 Dec 11, 2021
43c3494
Merge branch 'main' into diagram
yjt98765 Dec 15, 2021
c0ccbcf
Merge branch 'main' into diagram
yjt98765 Dec 17, 2021
e6f9bf2
Merge branch 'main' into diagram
yjt98765 Dec 22, 2021
d55bfd9
Merge branch 'main' into diagram
yjt98765 Dec 29, 2021
9660d95
Merge branch 'main' into diagram
yjt98765 Jan 4, 2022
249f920
Merge branch 'main' into diagram
yjt98765 Jan 5, 2022
457c02c
Merge branch 'main' into diagram
1ucian0 Jan 31, 2022
65e2828
Merge branch 'main' into diagram
1ucian0 Feb 4, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def _rescale_amplitudes(circuit: QuantumCircuit, scaling_factor: float) -> Quant

For example, for a scaling factor of 0.25 this turns this circuit

.. code-block::
.. parsed-literal::

┌───┐
state_0: ─────┤ H ├─────────■────
Expand All @@ -231,7 +231,7 @@ def _rescale_amplitudes(circuit: QuantumCircuit, scaling_factor: float) -> Quant

into

.. code-block::
.. parsed-literal::

┌───┐
state_0: ─────┤ H ├─────────■────
Expand Down
2 changes: 1 addition & 1 deletion qiskit/circuit/library/n_local/pauli_two_design.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class PauliTwoDesign(TwoLocal):
For instance, the circuit could look like this (but note that choosing a different seed
yields different Pauli rotations).

.. code-block::
.. parsed-literal::

┌─────────┐┌──────────┐ ░ ┌──────────┐ ░ ┌──────────┐
q_0: ┤ RY(π/4) ├┤ RZ(θ[0]) ├─■─────░─┤ RY(θ[4]) ├─■─────░──┤ RZ(θ[8]) ├
Expand Down
423 changes: 370 additions & 53 deletions qiskit/circuit/library/standard_gates/equivalence_library.py

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
def _apply_cu(circuit, theta, phi, lam, control, target, use_basis_gates=True):
if use_basis_gates:
# pylint: disable=cyclic-import
# ┌──────────────┐
# control: ┤ P(λ/2 + φ/2) ├──■──────────────────────────────────■────────────────
# ├──────────────┤┌─┴─┐┌────────────────────────────┐┌─┴─┐┌────────────┐
# target: ┤ P(λ/2 - φ/2) ├┤ X ├┤ U(-0.5*0,0,-0.5*λ - 0.5*φ) ├┤ X ├┤ U(0/2,φ,0) ├
# └──────────────┘└───┘└────────────────────────────┘└───┘└────────────┘
circuit.p((lam + phi) / 2, [control])
circuit.p((lam - phi) / 2, [target])
circuit.cx(control, target)
Expand Down
5 changes: 5 additions & 0 deletions qiskit/circuit/library/standard_gates/p.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,11 @@ def _define(self):
# pylint: disable=cyclic-import
from qiskit.circuit.quantumcircuit import QuantumCircuit

# ┌────────┐
# q_0: ┤ P(λ/2) ├──■──────────────■────────────
# └────────┘┌─┴─┐┌────────┐┌─┴─┐┌────────┐
# q_1: ──────────┤ X ├┤ P(λ/2) ├┤ X ├┤ P(λ/2) ├
# └───┘└────────┘└───┘└────────┘
q = QuantumRegister(2, "q")
qc = QuantumCircuit(q, name=self.name)
qc.p(self.params[0] / 2, 0)
Expand Down
4 changes: 4 additions & 0 deletions qiskit/circuit/library/standard_gates/rx.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ def _define(self):
from .u3 import U3Gate
from .x import CXGate

# q_0: ─────────────■───────────────────■────────────────────
# ┌─────────┐┌─┴─┐┌─────────────┐┌─┴─┐┌────────────────┐
# q_1: ┤ U1(π/2) ├┤ X ├┤ U3(0/2,0,0) ├┤ X ├┤ U3(0/2,-π/2,0) ├
# └─────────┘└───┘└─────────────┘└───┘└────────────────┘
q = QuantumRegister(2, "q")
qc = QuantumCircuit(q, name=self.name)
rules = [
Expand Down
5 changes: 5 additions & 0 deletions qiskit/circuit/library/standard_gates/rxx.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ def _define(self):
from .h import HGate
from .rz import RZGate

# ┌───┐ ┌───┐
# q_0: ┤ H ├──■─────────────■──┤ H ├
# ├───┤┌─┴─┐┌───────┐┌─┴─┐├───┤
# q_1: ┤ H ├┤ X ├┤ Rz(0) ├┤ X ├┤ H ├
# └───┘└───┘└───────┘└───┘└───┘
theta = self.params[0]
q = QuantumRegister(2, "q")
qc = QuantumCircuit(q, name=self.name)
Expand Down
4 changes: 4 additions & 0 deletions qiskit/circuit/library/standard_gates/ry.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ def _define(self):
from qiskit.circuit.quantumcircuit import QuantumCircuit
from .x import CXGate

# q_0: ─────────────■───────────────■──
# ┌─────────┐┌─┴─┐┌─────────┐┌─┴─┐
# q_1: ┤ Ry(λ/2) ├┤ X ├┤ Ry(λ/2) ├┤ X ├
# └─────────┘└───┘└─────────┘└───┘
q = QuantumRegister(2, "q")
qc = QuantumCircuit(q, name=self.name)
rules = [
Expand Down
5 changes: 5 additions & 0 deletions qiskit/circuit/library/standard_gates/ryy.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ def _define(self):
from .rx import RXGate
from .rz import RZGate

# ┌─────────┐ ┌──────────┐
# q_0: ┤ Rx(π/2) ├──■─────────────■──┤ Rx(-π/2) ├
# ├─────────┤┌─┴─┐┌───────┐┌─┴─┐├──────────┤
# q_1: ┤ Rx(π/2) ├┤ X ├┤ Rz(0) ├┤ X ├┤ Rx(-π/2) ├
# └─────────┘└───┘└───────┘└───┘└──────────┘
q = QuantumRegister(2, "q")
theta = self.params[0]
qc = QuantumCircuit(q, name=self.name)
Expand Down
4 changes: 4 additions & 0 deletions qiskit/circuit/library/standard_gates/rz.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ def _define(self):
from qiskit.circuit.quantumcircuit import QuantumCircuit
from .x import CXGate

# q_0: ─────────────■────────────────■──
# ┌─────────┐┌─┴─┐┌──────────┐┌─┴─┐
# q_1: ┤ Rz(λ/2) ├┤ X ├┤ Rz(-λ/2) ├┤ X ├
# └─────────┘└───┘└──────────┘└───┘
q = QuantumRegister(2, "q")
qc = QuantumCircuit(q, name=self.name)
rules = [
Expand Down
4 changes: 4 additions & 0 deletions qiskit/circuit/library/standard_gates/rzx.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ def _define(self):
from .x import CXGate
from .rz import RZGate

# q_0: ───────■─────────────■───────
# ┌───┐┌─┴─┐┌───────┐┌─┴─┐┌───┐
# q_1: ┤ H ├┤ X ├┤ Rz(0) ├┤ X ├┤ H ├
# └───┘└───┘└───────┘└───┘└───┘
theta = self.params[0]
q = QuantumRegister(2, "q")
qc = QuantumCircuit(q, name=self.name)
Expand Down
4 changes: 4 additions & 0 deletions qiskit/circuit/library/standard_gates/rzz.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ def _define(self):
from .x import CXGate
from .rz import RZGate

# q_0: ──■─────────────■──
# ┌─┴─┐┌───────┐┌─┴─┐
# q_1: ┤ X ├┤ Rz(0) ├┤ X ├
# └───┘└───────┘└───┘
q = QuantumRegister(2, "q")
theta = self.params[0]
qc = QuantumCircuit(q, name=self.name)
Expand Down
5 changes: 5 additions & 0 deletions qiskit/circuit/library/standard_gates/u.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,11 @@ def _define(self):
# pylint: disable=cyclic-import
from qiskit.circuit.quantumcircuit import QuantumCircuit

# ┌──────┐ ┌──────────────┐
# q_0: ────┤ P(γ) ├────┤ P(λ/2 + φ/2) ├──■────────────────────────────■────────────────
# ┌───┴──────┴───┐└──────────────┘┌─┴─┐┌──────────────────────┐┌─┴─┐┌────────────┐
# q_1: ┤ P(λ/2 - φ/2) ├────────────────┤ X ├┤ U(-0/2,0,-λ/2 - φ/2) ├┤ X ├┤ U(0/2,φ,0) ├
# └──────────────┘ └───┘└──────────────────────┘└───┘└────────────┘
q = QuantumRegister(2, "q")
qc = QuantumCircuit(q, name=self.name)
qc.p(self.params[3], 0)
Expand Down
5 changes: 5 additions & 0 deletions qiskit/circuit/library/standard_gates/u1.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@ def _define(self):
from qiskit.circuit.quantumcircuit import QuantumCircuit
from .x import CXGate # pylint: disable=cyclic-import

# ┌─────────┐
# q_0: ┤ U1(λ/2) ├──■────────────────■─────────────
# └─────────┘┌─┴─┐┌──────────┐┌─┴─┐┌─────────┐
# q_1: ───────────┤ X ├┤ U1(-λ/2) ├┤ X ├┤ U1(λ/2) ├
# └───┘└──────────┘└───┘└─────────┘
q = QuantumRegister(2, "q")
qc = QuantumCircuit(q, name=self.name)
rules = [
Expand Down
5 changes: 5 additions & 0 deletions qiskit/circuit/library/standard_gates/u3.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,11 @@ def _define(self):
from .u1 import U1Gate
from .x import CXGate # pylint: disable=cyclic-import

# ┌───────────────┐
# q_0: ┤ U1(λ/2 + φ/2) ├──■─────────────────────────────■─────────────────
# ├───────────────┤┌─┴─┐┌───────────────────────┐┌─┴─┐┌─────────────┐
# q_1: ┤ U1(λ/2 - φ/2) ├┤ X ├┤ U3(-0/2,0,-λ/2 - φ/2) ├┤ X ├┤ U3(0/2,φ,0) ├
# └───────────────┘└───┘└───────────────────────┘└───┘└─────────────┘
q = QuantumRegister(2, "q")
qc = QuantumCircuit(q, name=self.name)
rules = [
Expand Down
7 changes: 7 additions & 0 deletions qiskit/circuit/library/standard_gates/x.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,13 @@ def _define(self):
# pylint: disable=cyclic-import
from qiskit.circuit.quantumcircuit import QuantumCircuit

# ┌───┐
# q_0: ───────────────────■─────────────────────■────■───┤ T ├───■──
# │ ┌───┐ │ ┌─┴─┐┌┴───┴┐┌─┴─┐
# q_1: ───────■───────────┼─────────■───┤ T ├───┼──┤ X ├┤ Tdg ├┤ X ├
# ┌───┐┌─┴─┐┌─────┐┌─┴─┐┌───┐┌─┴─┐┌┴───┴┐┌─┴─┐├───┤└┬───┬┘└───┘
# q_2: ┤ H ├┤ X ├┤ Tdg ├┤ X ├┤ T ├┤ X ├┤ Tdg ├┤ X ├┤ T ├─┤ H ├──────
# └───┘└───┘└─────┘└───┘└───┘└───┘└─────┘└───┘└───┘ └───┘
q = QuantumRegister(3, "q")
qc = QuantumCircuit(q, name=self.name)
rules = [
Expand Down
86 changes: 52 additions & 34 deletions qiskit/circuit/quantumcircuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,18 +425,24 @@ def reverse_ops(self) -> "QuantumCircuit":
Examples:

input:
┌───┐
q_0: ┤ H ├─────■──────
└───┘┌────┴─────┐
q_1: ─────┤ RX(1.57) ├
└──────────┘

.. parsed-literal::

┌───┐
q_0: ┤ H ├─────■──────
└───┘┌────┴─────┐
q_1: ─────┤ RX(1.57) ├
└──────────┘

output:
┌───┐
q_0: ─────■──────┤ H ├
┌────┴─────┐└───┘
q_1: ┤ RX(1.57) ├─────
└──────────┘

.. parsed-literal::

┌───┐
q_0: ─────■──────┤ H ├
┌────┴─────┐└───┘
q_1: ┤ RX(1.57) ├─────
└──────────┘
"""
reverse_circ = QuantumCircuit(
self.qubits, self.clbits, *self.qregs, *self.cregs, name=self.name + "_reverse"
Expand Down Expand Up @@ -465,18 +471,24 @@ def reverse_bits(self) -> "QuantumCircuit":
Examples:

input:
┌───┐
q_0: ┤ H ├─────■──────
└───┘┌────┴─────┐
q_1: ─────┤ RX(1.57) ├
└──────────┘

.. parsed-literal::

┌───┐
q_0: ┤ H ├─────■──────
└───┘┌────┴─────┐
q_1: ─────┤ RX(1.57) ├
└──────────┘

output:
┌──────────┐
q_0: ─────┤ RX(1.57) ├
┌───┐└────┬─────┘
q_1: ┤ H ├─────■──────
└───┘

.. parsed-literal::

┌──────────┐
q_0: ─────┤ RX(1.57) ├
┌───┐└────┬─────┘
q_1: ┤ H ├─────■──────
└───┘
"""
circ = QuantumCircuit(
*reversed(self.qregs),
Expand Down Expand Up @@ -511,18 +523,24 @@ def inverse(self) -> "QuantumCircuit":
Examples:

input:
┌───┐
q_0: ┤ H ├─────■──────
└───┘┌────┴─────┐
q_1: ─────┤ RX(1.57) ├
└──────────┘

.. parsed-literal::

┌───┐
q_0: ┤ H ├─────■──────
└───┘┌────┴─────┐
q_1: ─────┤ RX(1.57) ├
└──────────┘

output:
┌───┐
q_0: ──────■──────┤ H ├
┌─────┴─────┐└───┘
q_1: ┤ RX(-1.57) ├─────
└───────────┘

.. parsed-literal::

┌───┐
q_0: ──────■──────┤ H ├
┌─────┴─────┐└───┘
q_1: ┤ RX(-1.57) ├─────
└───────────┘
"""
inverse_circ = QuantumCircuit(
self.qubits,
Expand Down Expand Up @@ -910,11 +928,11 @@ def tensor(self, other: "QuantumCircuit", inplace: bool = False) -> Optional["Qu

.. parsed-literal::

┌────────┐ ┌─────┐ ┌─────┐
┌────────┐ ┌─────┐ ┌─────┐
q_0: ┤ bottom ├ ⊗ q_0: ┤ top ├ = q_0: ─┤ top ├──
└────────┘ └─────┘ ┌┴─────┴─┐
q_1: ┤ bottom ├
└────────┘
└────────┘ └─────┘ ┌┴─────┴─┐
q_1: ┤ bottom ├
└────────┘

Args:
other (QuantumCircuit): The other circuit to tensor this circuit with.
Expand Down
34 changes: 21 additions & 13 deletions qiskit/extensions/quantum_initializer/initializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,9 +404,12 @@ def initialize(self, params, qubits=None):
circuit.draw()

output:
┌──────────────────────────────┐
q_0: ┤ initialize(0.70711,-0.70711) ├
└──────────────────────────────┘

.. parsed-literal::

┌──────────────────────────────┐
q_0: ┤ initialize(0.70711,-0.70711) ├
└──────────────────────────────┘


Initialize from a string two qubits in the state `|10>`.
Expand All @@ -424,12 +427,14 @@ def initialize(self, params, qubits=None):
circuit.draw()

output:
┌──────────────────┐
q_0: ┤0 ├
│ initialize(0,1) │
q_1: ┤1 ├
└──────────────────┘

.. parsed-literal::

┌──────────────────┐
q_0: ┤0 ├
│ initialize(0,1) │
q_1: ┤1 ├
└──────────────────┘

Initialize two qubits from an array of complex amplitudes
.. jupyter-execute::
Expand All @@ -442,11 +447,14 @@ def initialize(self, params, qubits=None):
circuit.draw()

output:
┌────────────────────────────────────┐
q_0: ┤0 ├
│ initialize(0,0.70711,-0.70711j,0) │
q_1: ┤1 ├
└────────────────────────────────────┘

.. parsed-literal::

┌────────────────────────────────────┐
q_0: ┤0 ├
│ initialize(0,0.70711,-0.70711j,0) │
q_1: ┤1 ├
└────────────────────────────────────┘
"""
if qubits is None:
qubits = self.qubits
Expand Down
Loading