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

Gate documentation overhaul #4017

Merged
merged 40 commits into from
Mar 26, 2020
Merged
Show file tree
Hide file tree
Changes from 39 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
c560009
docs for x.py
ajavadia Mar 23, 2020
907fdd9
update rzz docstring
ajavadia Mar 23, 2020
ce54023
dcx docs
ajavadia Mar 24, 2020
9eba3d1
add RZXGate
ajavadia Mar 25, 2020
74901fe
polish doc for x,y,z,rzz,rzx,iswap,dcx
ajavadia Mar 25, 2020
200ac0e
i gate
ajavadia Mar 25, 2020
8d3c150
u1 and rz docs
ajavadia Mar 25, 2020
7f4418f
u2 and u3
ajavadia Mar 25, 2020
cab7923
rx,ry,rz
ajavadia Mar 25, 2020
f2219c4
fix typos
ajavadia Mar 25, 2020
02f6615
cswap
ajavadia Mar 25, 2020
cbf0fbc
s and t
ajavadia Mar 25, 2020
ab24004
tweak
ajavadia Mar 25, 2020
4ebeb61
lint
ajavadia Mar 25, 2020
9a77830
release notes
ajavadia Mar 25, 2020
d2ed88e
iswap def in terms of xx+yy
ajavadia Mar 25, 2020
58f674f
lint
ajavadia Mar 25, 2020
668fe55
correct phase in RY definition and add reference implementation for i…
ajavadia Mar 26, 2020
b05187d
change u1->rz in RZZ definition
ajavadia Mar 26, 2020
906dac9
Update qiskit/extensions/standard/rzx.py
ajavadia Mar 26, 2020
4290548
Update qiskit/extensions/standard/rz.py
ajavadia Mar 26, 2020
b437485
Update rz.py
ajavadia Mar 26, 2020
c43811b
Merge branch 'master' into gate-documentation
Cryoris Mar 26, 2020
a8b511d
Update qiskit/extensions/standard/rxx.py
ajavadia Mar 26, 2020
408e322
Update qiskit/extensions/standard/iswap.py
ajavadia Mar 26, 2020
a2095bb
Update qiskit/extensions/standard/ry.py
ajavadia Mar 26, 2020
81b8c7a
Update qiskit/extensions/standard/rzx.py
ajavadia Mar 26, 2020
8bc124c
Update qiskit/extensions/standard/rzx.py
ajavadia Mar 26, 2020
4d7986f
Update qiskit/extensions/standard/u1.py
ajavadia Mar 26, 2020
2c99811
Update qiskit/extensions/standard/u3.py
ajavadia Mar 26, 2020
c604264
Update qiskit/extensions/standard/x.py
ajavadia Mar 26, 2020
3e8d8a2
Update qiskit/extensions/standard/x.py
ajavadia Mar 26, 2020
3840035
Update qiskit/extensions/standard/x.py
ajavadia Mar 26, 2020
7f4c908
Update qiskit/extensions/standard/rzz.py
ajavadia Mar 26, 2020
743cd8f
Update qiskit/extensions/standard/x.py
ajavadia Mar 26, 2020
f7d7c01
Update qiskit/extensions/standard/y.py
ajavadia Mar 26, 2020
a77d823
Update qiskit/extensions/standard/y.py
ajavadia Mar 26, 2020
0469f3c
Update qiskit/extensions/standard/z.py
ajavadia Mar 26, 2020
0299f8c
Update qiskit/extensions/standard/y.py
ajavadia Mar 26, 2020
63c9aef
Merge branch 'master' into gate-documentation
mergify[bot] Mar 26, 2020
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
1 change: 1 addition & 0 deletions qiskit/extensions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
RYGate
RZGate
RZZGate
RZXGate
SGate
SdgGate
SwapGate
Expand Down
1 change: 1 addition & 0 deletions qiskit/extensions/standard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from .ryy import RYYGate
from .rz import RZGate, CRZGate
from .rzz import RZZGate
from .rzx import RZXGate
from .s import SGate, SdgGate
from .swap import SwapGate, CSwapGate
from .iswap import iSwapGate
Expand Down
25 changes: 3 additions & 22 deletions qiskit/extensions/standard/dcx.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class DCXGate(Gate):

.. math::

DCX =
DCX\ q_0, q_1 =
\begin{pmatrix}
1 & 0 & 0 & 0 \\
0 & 0 & 0 & 1 \\
Expand All @@ -50,6 +50,7 @@ class DCXGate(Gate):
"""

def __init__(self):
"""Create new DCX gate."""
super().__init__('dcx', 2, [])

def _define(self):
Expand All @@ -72,27 +73,7 @@ def to_matrix(self):


def dcx(self, qubit1, qubit2):
"""Apply DCX gate to a pair specified qubits (qubit1, qubit2).

Examples:

Circuit Representation:

.. jupyter-execute::

from qiskit import QuantumCircuit

circuit = QuantumCircuit(2)
circuit.dcx(0, 1)
print(circuit)
print(circuit.decompose())

Matrix Representation:

.. jupyter-execute::

from qiskit.extensions.standard.dcx import DCXGate
DCXGate().to_matrix()
"""Apply :class:`~qiskit.extensions.standard.DCXGate`.
"""
return self.append(DCXGate(), [qubit1, qubit2], [])

Expand Down
125 changes: 72 additions & 53 deletions qiskit/extensions/standard/h.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,33 @@

# pylint: disable=cyclic-import
class HGate(Gate):
"""Hadamard gate."""
r"""Single-qubit Hadamard gate.

This gate is a \pi rotation about the X+Z axis, and has the effect of
changing computation basis from :math:`|0\rangle,|1\rangle` to
:math:`|+\rangle,|-\rangle` and vice-versa.

**Circuit symbol:**

.. parsed-literal::

┌───┐
q_0: ┤ H ├
└───┘

**Matrix Representation:**

.. math::

H = \frac{1}{\sqrt{2}}
\begin{pmatrix}
1 & 1 \\
1 & -1
\end{pmatrix}
"""

def __init__(self, label=None):
"""Create new Hadamard gate."""
"""Create new H gate."""
super().__init__('h', 1, [], label=label)

def _define(self):
Expand All @@ -49,7 +72,9 @@ def _define(self):
self.definition = definition

def control(self, num_ctrl_qubits=1, label=None, ctrl_state=None):
"""Controlled version of this gate.
"""Return a (multi-)controlled-H gate.

One control qubit returns a CH gate.

Args:
num_ctrl_qubits (int): number of control qubits.
Expand All @@ -67,7 +92,7 @@ def control(self, num_ctrl_qubits=1, label=None, ctrl_state=None):
ctrl_state=ctrl_state)

def inverse(self):
"""Invert this gate."""
r"""Return inverted H gate (itself)."""
return HGate() # self-inverse

def to_matrix(self):
Expand All @@ -78,43 +103,62 @@ def to_matrix(self):

@deprecate_arguments({'q': 'qubit'})
def h(self, qubit, *, q=None): # pylint: disable=invalid-name,unused-argument
r"""Apply Hadamard (H) gate.
"""Apply :class:`~qiskit.extensions.standard.HGate`."""
return self.append(HGate(), [qubit], [])

Applied to a specified qubit ``qubit``.

An H gate implements a rotation of :math:`\pi` about the axis
:math:`\frac{(x + z)}{\sqrt{2}}` on the Bloch sphere. This gate is
canonically used to rotate the qubit state from :math:`|0\rangle` to
:math:`|+\rangle` or :math:`|1\rangle` to :math:`|-\rangle`.
QuantumCircuit.h = h

Examples:

Circuit Representation:
class CHGate(ControlledGate):
r"""Controlled-Hadamard gate.

.. jupyter-execute::
Applies a Hadamard on the target qubit if the control is
in the :math:`|1\rangle` state.

from qiskit import QuantumCircuit
**Circuit symbol:**

circuit = QuantumCircuit(1)
circuit.h(0)
circuit.draw()
.. parsed-literal::

Matrix Representation:
┌───┐
q_0: ┤ H ├
└─┬─┘
q_1: ──■──

.. jupyter-execute::
**Matrix Representation:**

from qiskit.extensions.standard.h import HGate
HGate().to_matrix()
.. math::

"""
return self.append(HGate(), [qubit], [])
CH\ q_1, q_0 =
|0\rangle\langle 0| \otimes I + |1\rangle\langle 1| \otimes H =
\frac{1}{\sqrt{2}}
\begin{pmatrix}
1 & 0 & 0 & 0 \\
0 & 1 & 0 & 0 \\
0 & 0 & 1 & 1 \\
0 & 0 & 1 & -1
\end{pmatrix}

.. note::

QuantumCircuit.h = h
In Qiskit's convention, higher qubit indices are more significant
(little endian convention). In many textbooks, controlled gates are
presented with the assumption of more significant qubits as control,
which is how we present the gate above as well, resulting in textbook
matrices. Instead, if we use q_0 as control, the matrix will be:

.. math::

class CHGate(ControlledGate):
"""The controlled-H gate."""
CH\ q_0, q_1 =
I \otimes |0\rangle\langle 0| + H \otimes |1\rangle\langle 1| =
\frac{1}{\sqrt{2}}
\begin{pmatrix}
1 & 0 & 0 & 0 \\
0 & 1 & 0 & 1 \\
0 & 0 & 1 & 0 \\
0 & 1 & 0 & -1
\end{pmatrix}
"""

def __init__(self):
"""Create new CH gate."""
Expand Down Expand Up @@ -150,7 +194,7 @@ def _define(self):
self.definition = definition

def inverse(self):
"""Invert this gate."""
"""Return inverted CH gate (itself)."""
return CHGate() # self-inverse

def to_matrix(self):
Expand All @@ -165,32 +209,7 @@ def to_matrix(self):
@deprecate_arguments({'ctl': 'control_qubit', 'tgt': 'target_qubit'})
def ch(self, control_qubit, target_qubit, # pylint: disable=invalid-name
*, ctl=None, tgt=None): # pylint: disable=unused-argument
"""Apply cH gate

From a specified control ``control_qubit`` to target ``target_qubit`` qubit.
This gate is canonically used to rotate the qubit state from :math:`|0\\rangle` to
:math:`|+\\rangle` and :math:`|1\\rangle to :math:`|−\\rangle` when the control qubit is
in state :math:`|1\\rangle`.

Examples:

Circuit Representation:

.. jupyter-execute::

from qiskit import QuantumCircuit

circuit = QuantumCircuit(2)
circuit.ch(0,1)
circuit.draw()

Matrix Representation:

.. jupyter-execute::

from qiskit.extensions.standard.h import CHGate
CHGate().to_matrix()
"""
"""Apply :class:`~qiskit.extensions.standard.CHGate`."""
return self.append(CHGate(), [control_qubit, target_qubit], [])


Expand Down
45 changes: 18 additions & 27 deletions qiskit/extensions/standard/i.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,26 @@ def __instancecheck__(mcs, inst):


class IGate(Gate, metaclass=IMeta):
"""Identity gate.
r"""Identity gate.

Identity gate corresponds to a single-qubit gate wait cycle,
and should not be optimized or unrolled (it is an opaque gate).

**Matrix Representation:**

.. math::

I = \begin{pmatrix}
1 & 0 \\
0 & 1
\end{pmatrix}

**Circuit symbol:**

.. parsed-literal::
┌───┐
q_0: ┤ I ├
└───┘
"""

def __init__(self, label=None):
Expand Down Expand Up @@ -66,32 +82,7 @@ def __init__(self):

@deprecate_arguments({'q': 'qubit'})
def i(self, qubit, *, q=None): # pylint: disable=unused-argument
"""Apply Identity to to a specified qubit ``qubit``.

The Identity gate ensures that nothing is applied to a qubit for one unit
of gate time. It leaves the quantum states :math:`|0\\rangle` and
:math:`|1\\rangle` unchanged. The Identity gate should not be optimized or
unrolled (it is an opaque gate).

Examples:

Circuit Representation:

.. jupyter-execute::

from qiskit import QuantumCircuit

circuit = QuantumCircuit(1)
circuit.id(0) # or circuit.i(0)
circuit.draw()

Matrix Representation:

.. jupyter-execute::

from qiskit.extensions.standard.i import IGate
IGate().to_matrix()
"""
"""Apply :class:`~qiskit.extensions.standard.IGate`."""
return self.append(IGate(), [qubit], [])


Expand Down
46 changes: 24 additions & 22 deletions qiskit/extensions/standard/iswap.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,21 @@
class iSwapGate(Gate):
r"""iSWAP gate.

A 2-qubit XY interaction that is equivalent to a SWAP up to a diagonal.
A 2-qubit XX+YY interaction.
This is a Clifford and symmetric gate. Its action is to swap two qubit
states and phase the :math:`|01\rangle` and :math:`|10\rangle`
amplitudes by i.

**Circuit Symbol:**

.. parsed-literal::

q_0: ─⨂─
q_1: ─⨂─

**Reference Implementation:**

.. parsed-literal::

┌───┐┌───┐ ┌───┐
Expand All @@ -38,16 +48,25 @@ class iSwapGate(Gate):
q_1: ┤ S ├─────┤ X ├──■──┤ H ├
└───┘ └───┘ └───┘

**Matrix Representation:**

.. math::

iSWAP =
iSWAP = R_{XX+YY}(-\frac{\pi}{2})
= exp(i \frac{\pi}{4} (X{\otimes}X+Y{\otimes}Y)) =
\begin{pmatrix}
1 & 0 & 0 & 0 \\
0 & 0 & i & 0 \\
0 & i & 0 & 0 \\
0 & 0 & 0 & 1
\end{pmatrix}
= \begin{pmatrix}

This gate is equivalent to a SWAP up to a diagonal.

.. math::

iSWAP =
\begin{pmatrix}
1 & 0 & 0 & 0 \\
0 & 0 & 1 & 0 \\
0 & 1 & 0 & 0 \\
Expand All @@ -62,6 +81,7 @@ class iSwapGate(Gate):
"""

def __init__(self):
"""Create new iSwap gate."""
super().__init__('iswap', 2, [])

def _define(self):
Expand Down Expand Up @@ -97,25 +117,7 @@ def to_matrix(self):


def iswap(self, qubit1, qubit2):
"""Apply iSWAP gate to a pair specified qubits (qubit1, qubit2).

Examples:

Circuit Representation:

.. jupyter-execute::

from qiskit import QuantumCircuit

circuit = QuantumCircuit(2)
circuit.iswap(0,1)
circuit.draw()

.. jupyter-execute::

from qiskit.extensions.standard.iswap import iSwapGate
from qiskit.quantum_info import Operator
Operator(iSwapGate()).data
"""Apply :class:`~qiskit.extensions.standard.iSwapGate`.
"""
return self.append(iSwapGate(), [qubit1, qubit2], [])

Expand Down
Loading