From 391bfc53385479446e8504ea76416170fd5a680a Mon Sep 17 00:00:00 2001 From: Dave Bacon Date: Thu, 7 Apr 2022 12:03:20 -0700 Subject: [PATCH 1/7] initial work --- cirq-core/cirq/ops/common_gates.py | 2 +- docs/gatezoo.ipynb | 352 +++++++++++++++++++++++++++++ docs/qubits.ipynb | 15 +- docs/qudits.ipynb | 15 +- 4 files changed, 381 insertions(+), 3 deletions(-) create mode 100644 docs/gatezoo.ipynb diff --git a/cirq-core/cirq/ops/common_gates.py b/cirq-core/cirq/ops/common_gates.py index 9ceebe1424c..6fb3f173a17 100644 --- a/cirq-core/cirq/ops/common_gates.py +++ b/cirq-core/cirq/ops/common_gates.py @@ -1224,7 +1224,7 @@ def cphase(rads: value.TParamVal) -> CZPowGate: [[s, s], [s, -s]] ``` - where s = sqrt(0.5). + where s = sqrt(0.5). """, ) diff --git a/docs/gatezoo.ipynb b/docs/gatezoo.ipynb new file mode 100644 index 00000000000..8a520230638 --- /dev/null +++ b/docs/gatezoo.ipynb @@ -0,0 +1,352 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "2e694355-6c8f-43c5-b1ae-4e86af9fc9eb", + "metadata": { + "cellView": "form", + "id": "KQa9t_gadIuR" + }, + "outputs": [], + "source": [ + "#@title Copyright 2022 The Cirq Developers\n", + "# Licensed under the Apache License, Version 2.0 (the \"License\");\n", + "# you may not use this file except in compliance with the License.\n", + "# You may obtain a copy of the License at\n", + "#\n", + "# https://www.apache.org/licenses/LICENSE-2.0\n", + "#\n", + "# Unless required by applicable law or agreed to in writing, software\n", + "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", + "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", + "# See the License for the specific language governing permissions and\n", + "# limitations under the License." + ] + }, + { + "cell_type": "markdown", + "id": "42a837dc-73a7-4a4e-a79b-563786f1bfc6", + "metadata": { + "id": "xwec7FrkdFmi" + }, + "source": [ + "# Gate Zoo" + ] + }, + { + "cell_type": "markdown", + "id": "1ffc5e6a-50af-495e-b713-ad7c626a6181", + "metadata": { + "id": "5KZia7jmdJ3V" + }, + "source": [ + "\n", + " \n", + " \n", + " \n", + " \n", + "
\n", + " View on QuantumAI\n", + " \n", + " Run in Google Colab\n", + " \n", + " View source on GitHub\n", + " \n", + " Download notebook\n", + "
" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "0b650764-128c-46fe-8b19-ee54762a77ef", + "metadata": { + "id": "bd9529db1c0b" + }, + "outputs": [], + "source": [ + "try:\n", + " import cirq\n", + "except ImportError:\n", + " print(\"installing cirq...\")\n", + " !pip install --quiet cirq\n", + " print(\"installed cirq.\")\n", + " import cirq" + ] + }, + { + "cell_type": "markdown", + "id": "5731f798-b633-4039-a138-8c70b4d8fac4", + "metadata": {}, + "source": [ + "Cirq comes with many gates that are standard across quantum computing. This notebook serves as a reference sheet for these gates.\n", + "\n", + "## Single Qubit Gates\n", + "\n", + "Cirq defines constants which are gate instances for particular important single qubit gates." + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "id": "6cb68f8a-4285-42ba-9a0f-b73bf13cd4f1", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Gate: X\n", + "The Pauli X gate.\n", + "\n", + " Matrix:\n", + "\n", + " [[0, 1],\n", + " [1, 0]]\n", + " \n", + "\n", + "Gate: Y\n", + "The Pauli Y gate.\n", + "\n", + " Matrix:\n", + "\n", + " [[0, -i],\n", + " [i, 0]]\n", + " \n", + "\n", + "Gate: Z\n", + "The Pauli Z gate.\n", + "\n", + " Matrix:\n", + "\n", + " [[1, 0],\n", + " [0, -1]]\n", + " \n", + "\n", + "Gate: H\n", + "The Hadamard gate.\n", + "\n", + " The `exponent=1` instance of `cirq.HPowGate`.\n", + "\n", + " Matrix:\n", + " ```\n", + " [[s, s],\n", + " [s, -s]]\n", + " ```\n", + " where s = sqrt(0.5).\n", + " \n", + "\n", + "Gate: S\n", + "The Clifford S gate.\n", + "\n", + " The `exponent=0.5` instance of `cirq.ZPowGate`.\n", + "\n", + " Matrix:\n", + " ```\n", + " [[1, 0],\n", + " [0, i]]\n", + " ```\n", + " \n", + "\n", + "Gate: T\n", + "The non-Clifford T gate.\n", + "\n", + " The `exponent=0.25` instance of `cirq.ZPowGate`.\n", + "\n", + " Matrix:\n", + " ```\n", + " [[1, 0]\n", + " [0, exp(i pi / 4)]]\n", + " ```\n", + " \n", + "\n" + ] + } + ], + "source": [ + "constant_gates = [\"X\", \"Y\", \"Z\", \"H\", \"S\", \"T\"]\n", + "for gate_name in constant_gates:\n", + " gate = getattr(cirq, gate_name)\n", + " print(f\"Gate: {gate_name}\")\n", + " print(gate.__doc__)\n", + " print()" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "e5d38c50-1774-4acf-b22c-47cd103c8a8d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "cirq.Y\n", + "The Pauli Y gate.\n", + "\n", + " Matrix:\n", + "\n", + " [[0, -i],\n", + " [i, 0]]\n", + " \n" + ] + } + ], + "source": [ + "print(\"cirq.Y\")\n", + "print(cirq.Y.__doc__)" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "id": "e478b06c-3a62-4b6c-a614-12dc25182304", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['_XYZ',\n", + " '__abstractmethods__',\n", + " '__add__',\n", + " '__annotations__',\n", + " '__call__',\n", + " '__class__',\n", + " '__delattr__',\n", + " '__dict__',\n", + " '__dir__',\n", + " '__doc__',\n", + " '__eq__',\n", + " '__format__',\n", + " '__ge__',\n", + " '__getattribute__',\n", + " '__gt__',\n", + " '__hash__',\n", + " '__init__',\n", + " '__init_subclass__',\n", + " '__le__',\n", + " '__lt__',\n", + " '__module__',\n", + " '__mul__',\n", + " '__ne__',\n", + " '__neg__',\n", + " '__new__',\n", + " '__pow__',\n", + " '__reduce__',\n", + " '__reduce_ex__',\n", + " '__repr__',\n", + " '__rmul__',\n", + " '__setattr__',\n", + " '__sizeof__',\n", + " '__str__',\n", + " '__sub__',\n", + " '__subclasshook__',\n", + " '__truediv__',\n", + " '__weakref__',\n", + " '_abc_impl',\n", + " '_apply_unitary_',\n", + " '_approx_eq_',\n", + " '_backwards_compatibility_num_qubits',\n", + " '_canonical_exponent',\n", + " '_canonical_exponent_cached',\n", + " '_circuit_diagram_info_',\n", + " '_commutes_',\n", + " '_commutes_on_qids_',\n", + " '_decompose_into_clifford_with_qubits_',\n", + " '_default_shape_from_num_qubits',\n", + " '_diagram_exponent',\n", + " '_eigen_components',\n", + " '_eigen_shifts',\n", + " '_equal_up_to_global_phase_',\n", + " '_exponent',\n", + " '_format_exponent_as_angle',\n", + " '_from_json_dict_',\n", + " '_global_shift',\n", + " '_has_stabilizer_effect_',\n", + " '_has_unitary_',\n", + " '_implemented_by_',\n", + " '_index',\n", + " '_is_parameterized_',\n", + " '_json_dict_',\n", + " '_mul_with_qubits',\n", + " '_name',\n", + " '_num_qubits_',\n", + " '_num_qubits_from_shape',\n", + " '_num_qubits_proto_from_num_qubits',\n", + " '_parameter_names_',\n", + " '_pauli_expansion_',\n", + " '_period',\n", + " '_phase_by_',\n", + " '_qasm_',\n", + " '_qid_shape_',\n", + " '_quil_',\n", + " '_resolve_parameters_',\n", + " '_rmul_with_qubits',\n", + " '_trace_distance_bound_',\n", + " '_unitary_',\n", + " '_value_equality_approximate_values_',\n", + " '_value_equality_values_',\n", + " '_value_equality_values_cls_',\n", + " '_with_exponent',\n", + " 'basis',\n", + " 'by_index',\n", + " 'by_relative_index',\n", + " 'controlled',\n", + " 'exponent',\n", + " 'global_shift',\n", + " 'in_su2',\n", + " 'num_qubits',\n", + " 'on',\n", + " 'on_each',\n", + " 'phase_exponent',\n", + " 'phased_pauli_product',\n", + " 'relative_index',\n", + " 'third',\n", + " 'validate_args',\n", + " 'with_canonical_global_phase',\n", + " 'with_probability',\n", + " 'wrap_in_linear_combination']" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dir(cirq.X)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7a560cfb-6d7b-4837-85d1-6aa96accc264", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.9" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/docs/qubits.ipynb b/docs/qubits.ipynb index 2e93404a2b8..1e180cd9bc2 100644 --- a/docs/qubits.ipynb +++ b/docs/qubits.ipynb @@ -145,9 +145,22 @@ }, "kernelspec": { "display_name": "Python 3", + "language": "python", "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.9" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 4 } diff --git a/docs/qudits.ipynb b/docs/qudits.ipynb index 36cef899b65..11c209cbea5 100644 --- a/docs/qudits.ipynb +++ b/docs/qudits.ipynb @@ -209,9 +209,22 @@ }, "kernelspec": { "display_name": "Python 3", + "language": "python", "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.9" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 4 } From b1fb0a636bd25e214ad35711daf8df286d2fb8a7 Mon Sep 17 00:00:00 2001 From: Dave Bacon Date: Mon, 11 Apr 2022 17:07:44 -0700 Subject: [PATCH 2/7] Clean up single qubit docs, add gate zoo --- cirq-core/cirq/ops/common_gates.py | 206 ++--- cirq-core/cirq/ops/pauli_gates.py | 39 +- cirq-core/cirq/ops/phased_x_gate.py | 16 +- cirq-core/cirq/ops/phased_x_z_gate.py | 19 +- docs/custom_gates.ipynb | 15 +- docs/gates.ipynb | 15 +- docs/gatezoo.ipynb | 1030 ++++++++++++++++++++----- docs/qudits.ipynb | 6 +- 8 files changed, 1024 insertions(+), 322 deletions(-) diff --git a/cirq-core/cirq/ops/common_gates.py b/cirq-core/cirq/ops/common_gates.py index 6fb3f173a17..7a514aac935 100644 --- a/cirq-core/cirq/ops/common_gates.py +++ b/cirq-core/cirq/ops/common_gates.py @@ -68,26 +68,23 @@ def _pi(rads): @value.value_equality class XPowGate(eigen_gate.EigenGate, gate_features.SingleQubitGate): - """A gate that rotates around the X axis of the Bloch sphere. + r"""A gate that rotates around the X axis of the Bloch sphere. - The unitary matrix of ``XPowGate(exponent=t)`` is: - - [[g·c, -i·g·s], - [-i·g·s, g·c]] - - where: - - c = cos(π·t/2) - s = sin(π·t/2) - g = exp(i·π·t/2). + The unitary matrix of `cirq.XPowGate(exponent=t)` is: + $$ + \begin{bmatrix} + e^{i \pi t /2} \cos(\pi t) & -i e^{i \pi t /2} \sin(\pi t) \\ + -i e^{i \pi t /2} \sin(\pi t) & e^{i \pi t /2} \cos(\pi t) + \end{bmatrix} + $$ Note in particular that this gate has a global phase factor of - e^{i·π·t/2} vs the traditionally defined rotation matrices - about the Pauli X axis. See `cirq.rx` for rotations without the global + $e^{i \pi t / 2}$ vs the traditionally defined rotation matrices + about the Pauli X axis. See `cirq.Rx` for rotations without the global phase. The global phase factor can be adjusted by using the `global_shift` parameter when initializing. - `cirq.X`, the Pauli X gate, is an instance of this gate at exponent=1. + `cirq.X`, the Pauli X gate, is an instance of this gate at `exponent=1`. """ def _apply_unitary_(self, args: 'protocols.ApplyUnitaryArgs') -> Optional[np.ndarray]: @@ -250,14 +247,18 @@ def __repr__(self) -> str: class Rx(XPowGate): - """A gate, with matrix e^{-i X rads/2}, that rotates around the X axis of the Bloch sphere. - - The unitary matrix of ``Rx(rads=t)`` is: - - exp(-i X t/2) = [ cos(t/2) -isin(t/2)] - [-isin(t/2) cos(t/2) ] - - The gate corresponds to the traditionally defined rotation matrices about the Pauli X axis. + r"""A gate, with matrix $e^{-i X t/2}$ that rotates around the X axis of the Bloch sphere by $t$. + + The unitary matrix of `cirq.Rx(rads=t)` is: + $$ + e^{-i X t /2} = + \begin{bmatrix} + \cos(t/2) & -i \sin(t/2) \\ + -i \sin(t/2) & \cos(t/2) + \end{bmatrix} + $$ + + This gate corresponds to the traditionally defined rotation matrices about the Pauli X axis. """ def __init__(self, *, rads: value.TParamVal): @@ -293,26 +294,23 @@ def _from_json_dict_(cls, rads, **kwargs) -> 'Rx': @value.value_equality class YPowGate(eigen_gate.EigenGate, gate_features.SingleQubitGate): - """A gate that rotates around the Y axis of the Bloch sphere. - - The unitary matrix of ``YPowGate(exponent=t)`` is: + r"""A gate that rotates around the Y axis of the Bloch sphere. - [[g·c, -g·s], - [g·s, g·c]] - - where: - - c = cos(π·t/2) - s = sin(π·t/2) - g = exp(i·π·t/2). + The unitary matrix of `cirq.YPowGate(exponent=t)` is: + $$ + \begin{bmatrix} + e^{i \pi t /2} \cos(\pi t /2) & - e^{i \pi t /2} \sin(\pi t /2) \\ + e^{i \pi t /2} \sin(\pi t /2) & e^{i \pi t /2} \cos(\pi t /2) + \end{bmatrix} + $$ Note in particular that this gate has a global phase factor of - e^{i·π·t/2} vs the traditionally defined rotation matrices + $e^{i \pi t / 2}$ vs the traditionally defined rotation matrices about the Pauli Y axis. See `cirq.Ry` for rotations without the global phase. The global phase factor can be adjusted by using the `global_shift` parameter when initializing. - `cirq.Y`, the Pauli Y gate, is an instance of this gate at exponent=1. + `cirq.Y`, the Pauli Y gate, is an instance of this gate at `exponent=1`. """ def _apply_unitary_(self, args: 'protocols.ApplyUnitaryArgs') -> Optional[np.ndarray]: @@ -425,14 +423,18 @@ def __repr__(self) -> str: class Ry(YPowGate): - """A gate, with matrix e^{-i Y rads/2}, that rotates around the Y axis of the Bloch sphere. - - The unitary matrix of ``Ry(rads=t)`` is: - - exp(-i Y t/2) = [cos(t/2) -sin(t/2)] - [sin(t/2) cos(t/2) ] - - The gate corresponds to the traditionally defined rotation matrices about the Pauli Y axis. + r"""A gate with matrix $e^{-i Y t/2}$ that rotates around the Y axis of the Bloch sphere by $t$. + + The unitary matrix of `cirq.Ry(rads=t)` is: + $$ + e^{-i Y t / 2} = + \begin{bmatrix} + \cos(t/2) & -\sin(t/2) \\ + \sin(t/2) & \cos(t/2) + \end{bmatrix} + $$ + + This gate corresponds to the traditionally defined rotation matrices about the Pauli Y axis. """ def __init__(self, *, rads: value.TParamVal): @@ -468,24 +470,23 @@ def _from_json_dict_(cls, rads, **kwargs) -> 'Ry': @value.value_equality class ZPowGate(eigen_gate.EigenGate, gate_features.SingleQubitGate): - """A gate that rotates around the Z axis of the Bloch sphere. - - The unitary matrix of ``ZPowGate(exponent=t)`` is: + r"""A gate that rotates around the Z axis of the Bloch sphere. - [[1, 0], - [0, g]] - - where: - - g = exp(i·π·t). + The unitary matrix of `cirq.ZPowGate(exponent=t)` is: + $$ + \begin{bmatrix} + 1 & 0 \\ + 0 & e^{i \pi t} + \end{bmatrix} + $$ Note in particular that this gate has a global phase factor of - e^{i·π·t/2} vs the traditionally defined rotation matrices + $e^{i\pi t/2}$ vs the traditionally defined rotation matrices about the Pauli Z axis. See `cirq.Rz` for rotations without the global phase. The global phase factor can be adjusted by using the `global_shift` parameter when initializing. - `cirq.Z`, the Pauli Z gate, is an instance of this gate at exponent=1. + `cirq.Z`, the Pauli Z gate, is an instance of this gate at `exponent=1`. """ def _apply_unitary_(self, args: 'protocols.ApplyUnitaryArgs') -> Optional[np.ndarray]: @@ -674,14 +675,18 @@ def _commutes_on_qids_( class Rz(ZPowGate): - """A gate, with matrix e^{-i Z rads/2}, that rotates around the Z axis of the Bloch sphere. - - The unitary matrix of ``Rz(rads=t)`` is: - - exp(-i Z t/2) = [ e^(-it/2) 0 ] - [ 0 e^(it/2)] - - The gate corresponds to the traditionally defined rotation matrices about the Pauli Z axis. + r"""A gate with matrix $e^{-i Z t/2}$ that rotates around the Z axis of the Bloch sphere by $t$. + + The unitary matrix of `cirq.Rz(rads=t)` is: + $$ + e^{-i Z t /2} = + \begin{bmatrix} + e^{-it/2} & 0 \\ + 0 & e^{it/2} + \end{bmatrix} + $$ + + This gate corresponds to the traditionally defined rotation matrices about the Pauli Z axis. """ def __init__(self, *, rads: value.TParamVal): @@ -716,20 +721,24 @@ def _from_json_dict_(cls, rads, **kwargs) -> 'Rz': class HPowGate(eigen_gate.EigenGate, gate_features.SingleQubitGate): - """A Gate that performs a rotation around the X+Z axis of the Bloch sphere. - - The unitary matrix of ``HPowGate(exponent=t)`` is: - - [[g·(c-i·s/sqrt(2)), -i·g·s/sqrt(2)], - [-i·g·s/sqrt(2)], g·(c+i·s/sqrt(2))]] - - where - - c = cos(π·t/2) - s = sin(π·t/2) - g = exp(i·π·t/2). - - Note in particular that for `t=1`, this gives the Hadamard matrix. + r"""A Gate that performs a rotation around the X+Z axis of the Bloch sphere. + + The unitary matrix of `cirq.HPowGate(exponent=t)` is: + $$ + \begin{bmatrix} + e^{i\pi t/2} \left(\cos(\pi t/2) - i \frac{\sin (\pi t /2)}{\sqrt{2}}\right) + && -i e^{i\pi t/2} \frac{\sin(\pi t /2)}{\sqrt{2}} \\ + -i e^{i\pi t/2} \frac{\sin(\pi t /2)}{\sqrt{2}} + && e^{i\pi t/2} \left(\cos(\pi t/2) + i \frac{\sin (\pi t /2)}{\sqrt{2}}\right) + \end{bmatrix} + $$ + Note in particular that for $t=1$, this gives the Hadamard matrix + $$ + \begin{bmatrix} + \frac{1}{\sqrt{2}} & \frac{1}{\sqrt{2}} \\ + \frac{1}{\sqrt{2}} & -\frac{1}{\sqrt{2}} + \end{bmatrix} + $$ `cirq.H`, the Hadamard gate, is an instance of this gate at `exponent=1`. """ @@ -1019,7 +1028,7 @@ class CXPowGate(eigen_gate.EigenGate): or named arguments CNOT(control=q1, target=q2). (Mixing the two is not permitted.) - The unitary matrix of `CXPowGate(exponent=t)` is: + The unitary matrix of `cirq.CXPowGate(exponent=t)` is: [[1, 0, 0, 0], [0, 1, 0, 0], @@ -1215,46 +1224,51 @@ def cphase(rads: value.TParamVal) -> CZPowGate: H = HPowGate() document( H, - """The Hadamard gate. + r"""The Hadamard gate. The `exponent=1` instance of `cirq.HPowGate`. - Matrix: - ``` - [[s, s], - [s, -s]] - ``` - where s = sqrt(0.5). + The unitary matrix of `cirq.H` is: + $$ + \begin{bmatrix} + \frac{1}{\sqrt{2}} & \frac{1}{\sqrt{2}} \\ + \frac{1}{\sqrt{2}} & -\frac{1}{\sqrt{2}} + \end{bmatrix} + $$ """, ) S = ZPowGate(exponent=0.5) document( S, - """The Clifford S gate. + r"""The Clifford S gate. The `exponent=0.5` instance of `cirq.ZPowGate`. - Matrix: - ``` - [[1, 0], - [0, i]] - ``` + The unitary matrix of `cirq.S` is: + $$ + \begin{bmatrix} + 1 & 0 \\ + 0 & i + \end{bmatrix} + $$ """, ) T = ZPowGate(exponent=0.25) document( T, - """The non-Clifford T gate. + r"""The non-Clifford T gate. The `exponent=0.25` instance of `cirq.ZPowGate`. - Matrix: - ``` - [[1, 0] - [0, exp(i pi / 4)]] - ``` + The unitary matrix of `cirq.T` is + $$ + \begin{bmatrix} + 1 & 0 \\ + 0 & e^{i \pi /4} + \end{bmatrix} + $$ """, ) diff --git a/cirq-core/cirq/ops/pauli_gates.py b/cirq-core/cirq/ops/pauli_gates.py index f5cb6c1f4bd..0943868704c 100644 --- a/cirq-core/cirq/ops/pauli_gates.py +++ b/cirq-core/cirq/ops/pauli_gates.py @@ -189,36 +189,51 @@ def basis(self: '_PauliZ') -> Dict[int, '_ZEigenState']: X = _PauliX() document( X, - """The Pauli X gate. + r"""The Pauli X gate. - Matrix: + This is the `exponent=1` instance of the `cirq.XPowGate`. - [[0, 1], - [1, 0]] + The untary matrix of `cirq.X` is: + $$ + \begin{bmatrix} + 0 & 1 \\ + 1 & 0 + \end{bmatrix} + $$ """, ) Y = _PauliY() document( Y, - """The Pauli Y gate. + r"""The Pauli Y gate. - Matrix: + This is the `exponent=1` instance of the `cirq.YPowGate`. - [[0, -i], - [i, 0]] + The unitary matrix of `cirq.Y` is: + $$ + \begin{bmatrix} + 0 & -i \\ + i & 0 + \end{bmatrix} + $$ """, ) Z = _PauliZ() document( Z, - """The Pauli Z gate. + r"""The Pauli Z gate. - Matrix: + This is the `exponent=1` instance of the `cirq.ZPowGate`. - [[1, 0], - [0, -1]] + The unitary matrix of `cirq.Z` is: + $$ + \begin{bmatrix} + 1 & 0 \\ + 0 & -1 + \end{bmatrix} + $$ """, ) diff --git a/cirq-core/cirq/ops/phased_x_gate.py b/cirq-core/cirq/ops/phased_x_gate.py index ef17dfb583c..4fbf5a53452 100644 --- a/cirq-core/cirq/ops/phased_x_gate.py +++ b/cirq-core/cirq/ops/phased_x_gate.py @@ -27,7 +27,21 @@ @value.value_equality(manual_cls=True, approximate=True) class PhasedXPowGate(gate_features.SingleQubitGate): - """A gate equivalent to the circuit ───Z^-p───X^t───Z^p───.""" + r"""A gate equivalent to $Z^{p} X^t Z^{-p}$. + + The unitary matrix of `cirq.PhasedXPowGate(exponent=t, phase_exponent=p)` is: + $$ + \begin{bmatrix} + e^{i \pi t /2} \cos(\pi t/2) & -i e^{i \pi (t /2 - p)} \sin(\pi t /2) \\ + -i e^{i \pi (t /2 + p)} \sin(\pi t /2) & e^{i \pi t /2} \cos(\pi t/2) + \end{bmatrix} + $$ + + This gate is like an `cirq.XPowGate`, but which has been "phased", + by applying a `cirq.ZPowGate` before and after this gate. In the language + of the Bloch sphere, $p$ determines the axis in the XY plane about which + a rotation of amount determined by $t$ occurs. + """ def __init__( self, diff --git a/cirq-core/cirq/ops/phased_x_z_gate.py b/cirq-core/cirq/ops/phased_x_z_gate.py index 053e3bffddb..9b591593e2f 100644 --- a/cirq-core/cirq/ops/phased_x_z_gate.py +++ b/cirq-core/cirq/ops/phased_x_z_gate.py @@ -15,16 +15,23 @@ @value.value_equality(approximate=True) class PhasedXZGate(gate_features.SingleQubitGate): - """A single qubit operation expressed as $Z^z Z^a X^x Z^{-a}$. + r"""A single qubit gate equivalent to the circuit $Z^z Z^{a} X^x Z^{-a}$. - The above expression is a matrix multiplication with time going to the left. - In quantum circuit notation, this operation decomposes into this circuit: + The unitary matrix of `cirq.PhasedXZGate(x_exponent=x, z_exponent=z, axis_phase_exponent=a)` is: + $$ + \begin{bmatrix} + e^{i \pi x / 2} \cos(\pi x /2) & -i e^{i \pi (x/2 - a)} \sin(\pi x / 2) \\ + -i e^{i \pi (x/2 + z + a)} \sin(\pi x / 2) && e^{i \pi (x / 2 + z)} \cos(\pi x /2) + \end{bmatrix} + $$ - ───Z^(-a)──X^x──Z^a────Z^z─── + This gate can be thought of as a `cirq.PhasedXPowGate` followed by a `cirq.ZPowGate`. - The axis phase exponent (a) decides which axis in the XY plane to rotate + The axis phase exponent ($a$) decides which axis in the XY plane to rotate around. The amount of rotation around that axis is decided by the x - exponent (x). Then the z exponent (z) decides how much to phase the qubit. + exponent ($x$). Then the z exponent ($z$) decides how much to finally phase the qubit. + + Every single qubit gate can be written as a single `cirq.PhasedXZGate`. """ def __init__( diff --git a/docs/custom_gates.ipynb b/docs/custom_gates.ipynb index 47792628e99..1adc0f2f3e5 100644 --- a/docs/custom_gates.ipynb +++ b/docs/custom_gates.ipynb @@ -651,9 +651,22 @@ }, "kernelspec": { "display_name": "Python 3", + "language": "python", "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.9" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 4 } diff --git a/docs/gates.ipynb b/docs/gates.ipynb index 8bb21c4a25f..aefe3393dbc 100644 --- a/docs/gates.ipynb +++ b/docs/gates.ipynb @@ -347,9 +347,22 @@ }, "kernelspec": { "display_name": "Python 3", + "language": "python", "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.9" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 4 } diff --git a/docs/gatezoo.ipynb b/docs/gatezoo.ipynb index 8a520230638..a33f2313e43 100644 --- a/docs/gatezoo.ipynb +++ b/docs/gatezoo.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "2e694355-6c8f-43c5-b1ae-4e86af9fc9eb", "metadata": { "cellView": "form", @@ -72,7 +72,19 @@ " print(\"installing cirq...\")\n", " !pip install --quiet cirq\n", " print(\"installed cirq.\")\n", - " import cirq" + " \n", + "import IPython.display as ipd\n", + "import cirq\n", + "import inspect\n", + "\n", + "def display_gates(*gates):\n", + " for gate_name in gates:\n", + " ipd.display(ipd.Markdown(\"---\"))\n", + " gate = getattr(cirq, gate_name)\n", + " ipd.display(ipd.Markdown(f\"#### cirq.{gate_name}\"))\n", + " ipd.display(ipd.Markdown(inspect.cleandoc(gate.__doc__)))\n", + " else:\n", + " ipd.display(ipd.Markdown(\"---\")) " ] }, { @@ -84,248 +96,862 @@ "\n", "## Single Qubit Gates\n", "\n", + "\n", + "### Gate constants \n", + "\n", "Cirq defines constants which are gate instances for particular important single qubit gates." ] }, { "cell_type": "code", - "execution_count": 31, + "execution_count": 3, "id": "6cb68f8a-4285-42ba-9a0f-b73bf13cd4f1", "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "Gate: X\n", - "The Pauli X gate.\n", - "\n", - " Matrix:\n", - "\n", - " [[0, 1],\n", - " [1, 0]]\n", - " \n", - "\n", - "Gate: Y\n", - "The Pauli Y gate.\n", - "\n", - " Matrix:\n", - "\n", - " [[0, -i],\n", - " [i, 0]]\n", - " \n", - "\n", - "Gate: Z\n", - "The Pauli Z gate.\n", - "\n", - " Matrix:\n", - "\n", - " [[1, 0],\n", - " [0, -1]]\n", - " \n", - "\n", - "Gate: H\n", - "The Hadamard gate.\n", - "\n", - " The `exponent=1` instance of `cirq.HPowGate`.\n", - "\n", - " Matrix:\n", - " ```\n", - " [[s, s],\n", - " [s, -s]]\n", - " ```\n", - " where s = sqrt(0.5).\n", - " \n", - "\n", - "Gate: S\n", - "The Clifford S gate.\n", - "\n", - " The `exponent=0.5` instance of `cirq.ZPowGate`.\n", - "\n", - " Matrix:\n", - " ```\n", - " [[1, 0],\n", - " [0, i]]\n", - " ```\n", - " \n", - "\n", - "Gate: T\n", - "The non-Clifford T gate.\n", - "\n", - " The `exponent=0.25` instance of `cirq.ZPowGate`.\n", - "\n", - " Matrix:\n", - " ```\n", - " [[1, 0]\n", - " [0, exp(i pi / 4)]]\n", - " ```\n", - " \n", - "\n" - ] + "data": { + "text/markdown": [ + "---" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/markdown": [ + "#### cirq.X" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/markdown": [ + "The Pauli X gate.\n", + "\n", + "This is the `exponent=1` instance of the `cirq.XPowGate`.\n", + "\n", + "The untary matrix of `cirq.X` is:\n", + "$$\n", + "\\begin{bmatrix}\n", + " 0 & 1 \\\\\n", + " 1 & 0\n", + "\\end{bmatrix}\n", + "$$" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/markdown": [ + "---" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/markdown": [ + "#### cirq.Y" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/markdown": [ + "The Pauli Y gate.\n", + "\n", + "This is the `exponent=1` instance of the `cirq.YPowGate`.\n", + "\n", + "The unitary matrix of `cirq.Y` is:\n", + "$$\n", + "\\begin{bmatrix}\n", + " 0 & -i \\\\\n", + " i & 0\n", + "\\end{bmatrix}\n", + "$$" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/markdown": [ + "---" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/markdown": [ + "#### cirq.Z" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/markdown": [ + "The Pauli Z gate.\n", + "\n", + "This is the `exponent=1` instance of the `cirq.ZPowGate`.\n", + "\n", + "The unitary matrix of `cirq.Z` is:\n", + "$$\n", + "\\begin{bmatrix}\n", + " 1 & 0 \\\\\n", + " 0 & -1\n", + "\\end{bmatrix}\n", + "$$" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/markdown": [ + "---" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/markdown": [ + "#### cirq.H" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/markdown": [ + "The Hadamard gate.\n", + "\n", + "The `exponent=1` instance of `cirq.HPowGate`.\n", + "\n", + "The unitary matrix of `cirq.H` is:\n", + "$$\n", + "\\begin{bmatrix}\n", + " \\frac{1}{\\sqrt{2}} & \\frac{1}{\\sqrt{2}} \\\\\n", + " \\frac{1}{\\sqrt{2}} & -\\frac{1}{\\sqrt{2}}\n", + "\\end{bmatrix}\n", + "$$" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/markdown": [ + "---" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/markdown": [ + "#### cirq.S" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/markdown": [ + "The Clifford S gate.\n", + "\n", + "The `exponent=0.5` instance of `cirq.ZPowGate`.\n", + "\n", + "The unitary matrix of `cirq.S` is:\n", + "$$\n", + "\\begin{bmatrix}\n", + " 1 & 0 \\\\\n", + " 0 & i\n", + "\\end{bmatrix}\n", + "$$" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/markdown": [ + "---" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/markdown": [ + "#### cirq.T" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/markdown": [ + "The non-Clifford T gate.\n", + "\n", + "The `exponent=0.25` instance of `cirq.ZPowGate`.\n", + "\n", + "The unitary matrix of `cirq.T` is\n", + "$$\n", + "\\begin{bmatrix}\n", + " 1 & 0 \\\\\n", + " 0 & e^{i \\pi /4}\n", + "\\end{bmatrix}\n", + "$$" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/markdown": [ + "---" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" } ], "source": [ - "constant_gates = [\"X\", \"Y\", \"Z\", \"H\", \"S\", \"T\"]\n", - "for gate_name in constant_gates:\n", - " gate = getattr(cirq, gate_name)\n", - " print(f\"Gate: {gate_name}\")\n", - " print(gate.__doc__)\n", - " print()" + "display_gates(\"X\", \"Y\", \"Z\", \"H\", \"S\", \"T\")" + ] + }, + { + "cell_type": "markdown", + "id": "d65a3acf-4446-4d7b-9077-39cd0b1095b6", + "metadata": {}, + "source": [ + "### Traditional Pauli Rotation Gates\n", + "\n", + "Traditional single qubit rotations expressed in radians of rotation about Pauli axis are defined." ] }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 4, "id": "e5d38c50-1774-4acf-b22c-47cd103c8a8d", "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "cirq.Y\n", - "The Pauli Y gate.\n", - "\n", - " Matrix:\n", - "\n", - " [[0, -i],\n", - " [i, 0]]\n", - " \n" - ] + "data": { + "text/markdown": [ + "---" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/markdown": [ + "#### cirq.Rx" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/markdown": [ + "A gate, with matrix $e^{-i X t/2}$ that rotates around the X axis of the Bloch sphere by $t$.\n", + "\n", + "The unitary matrix of `cirq.Rx(rads=t)` is:\n", + "$$\n", + "e^{-i X t /2} =\n", + " \\begin{bmatrix}\n", + " \\cos(t/2) & -i \\sin(t/2) \\\\\n", + " -i \\sin(t/2) & \\cos(t/2)\n", + " \\end{bmatrix}\n", + "$$\n", + "\n", + "This gate corresponds to the traditionally defined rotation matrices about the Pauli X axis." + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/markdown": [ + "---" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/markdown": [ + "#### cirq.Ry" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/markdown": [ + "A gate with matrix $e^{-i Y t/2}$ that rotates around the Y axis of the Bloch sphere by $t$.\n", + "\n", + "The unitary matrix of `cirq.Ry(rads=t)` is:\n", + "$$\n", + "e^{-i Y t / 2} =\n", + " \\begin{bmatrix}\n", + " \\cos(t/2) & -\\sin(t/2) \\\\\n", + " \\sin(t/2) & \\cos(t/2)\n", + " \\end{bmatrix}\n", + "$$\n", + "\n", + "This gate corresponds to the traditionally defined rotation matrices about the Pauli Y axis." + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/markdown": [ + "---" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/markdown": [ + "#### cirq.Rz" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/markdown": [ + "A gate with matrix $e^{-i Z t/2}$ that rotates around the Z axis of the Bloch sphere by $t$.\n", + "\n", + "The unitary matrix of `cirq.Rz(rads=t)` is:\n", + "$$\n", + "e^{-i Z t /2} =\n", + " \\begin{bmatrix}\n", + " e^{-it/2} & 0 \\\\\n", + " 0 & e^{it/2}\n", + " \\end{bmatrix}\n", + "$$\n", + "\n", + "This gate corresponds to the traditionally defined rotation matrices about the Pauli Z axis." + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/markdown": [ + "---" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" } ], "source": [ - "print(\"cirq.Y\")\n", - "print(cirq.Y.__doc__)" + "display_gates(\"Rx\", \"Ry\", \"Rz\")" + ] + }, + { + "cell_type": "markdown", + "id": "9a7fec58-bbe6-4ec7-8158-5afe735ea7e2", + "metadata": {}, + "source": [ + "### Pauli PowGates\n", + "\n", + "If you think of the `cirq.Z` gate as phasing the state $|1\\rangle$ by $-1$, then you might think that the square root of this gate phases the state $|1\\rangle$ by $i=\\sqrt{-1}$. The `XPowGate`, `YPowGate` and `ZPowGate`s all act in this manner, phasing the state corresponding to their $-1$ eigenvalue by a prescribed amount. This ends up being the same as the `Rx`, `Ry`, and `Rz` up to a global phase." ] }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 5, "id": "e478b06c-3a62-4b6c-a614-12dc25182304", "metadata": {}, "outputs": [ { "data": { + "text/markdown": [ + "---" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/markdown": [ + "#### cirq.XPowGate" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/markdown": [ + "A gate that rotates around the X axis of the Bloch sphere.\n", + "\n", + "The unitary matrix of `cirq.XPowGate(exponent=t)` is:\n", + "$$\n", + "\\begin{bmatrix}\n", + " e^{i \\pi t /2} \\cos(\\pi t) & -i e^{i \\pi t /2} \\sin(\\pi t) \\\\\n", + " -i e^{i \\pi t /2} \\sin(\\pi t) & e^{i \\pi t /2} \\cos(\\pi t)\n", + "\\end{bmatrix}\n", + "$$\n", + "\n", + "Note in particular that this gate has a global phase factor of\n", + "$e^{i \\pi t / 2}$ vs the traditionally defined rotation matrices\n", + "about the Pauli X axis. See `cirq.Rx` for rotations without the global\n", + "phase. The global phase factor can be adjusted by using the `global_shift`\n", + "parameter when initializing.\n", + "\n", + "`cirq.X`, the Pauli X gate, is an instance of this gate at `exponent=1`." + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/markdown": [ + "---" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/markdown": [ + "#### cirq.YPowGate" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/markdown": [ + "A gate that rotates around the Y axis of the Bloch sphere.\n", + "\n", + "The unitary matrix of `cirq.YPowGate(exponent=t)` is:\n", + "$$\n", + " \\begin{bmatrix}\n", + " e^{i \\pi t /2} \\cos(\\pi t /2) & - e^{i \\pi t /2} \\sin(\\pi t /2) \\\\\n", + " e^{i \\pi t /2} \\sin(\\pi t /2) & e^{i \\pi t /2} \\cos(\\pi t /2)\n", + " \\end{bmatrix}\n", + "$$\n", + "\n", + "Note in particular that this gate has a global phase factor of\n", + "$e^{i \\pi t / 2}$ vs the traditionally defined rotation matrices\n", + "about the Pauli Y axis. See `cirq.Ry` for rotations without the global\n", + "phase. The global phase factor can be adjusted by using the `global_shift`\n", + "parameter when initializing.\n", + "\n", + "`cirq.Y`, the Pauli Y gate, is an instance of this gate at `exponent=1`." + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/markdown": [ + "---" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/markdown": [ + "#### cirq.ZPowGate" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/markdown": [ + "A gate that rotates around the Z axis of the Bloch sphere.\n", + "\n", + "The unitary matrix of `cirq.ZPowGate(exponent=t)` is:\n", + "$$\n", + " \\begin{bmatrix}\n", + " 1 & 0 \\\\\n", + " 0 & e^{i \\pi t}\n", + " \\end{bmatrix}\n", + "$$\n", + "\n", + "Note in particular that this gate has a global phase factor of\n", + "$e^{i\\pi t/2}$ vs the traditionally defined rotation matrices\n", + "about the Pauli Z axis. See `cirq.Rz` for rotations without the global\n", + "phase. The global phase factor can be adjusted by using the `global_shift`\n", + "parameter when initializing.\n", + "\n", + "`cirq.Z`, the Pauli Z gate, is an instance of this gate at `exponent=1`." + ], "text/plain": [ - "['_XYZ',\n", - " '__abstractmethods__',\n", - " '__add__',\n", - " '__annotations__',\n", - " '__call__',\n", - " '__class__',\n", - " '__delattr__',\n", - " '__dict__',\n", - " '__dir__',\n", - " '__doc__',\n", - " '__eq__',\n", - " '__format__',\n", - " '__ge__',\n", - " '__getattribute__',\n", - " '__gt__',\n", - " '__hash__',\n", - " '__init__',\n", - " '__init_subclass__',\n", - " '__le__',\n", - " '__lt__',\n", - " '__module__',\n", - " '__mul__',\n", - " '__ne__',\n", - " '__neg__',\n", - " '__new__',\n", - " '__pow__',\n", - " '__reduce__',\n", - " '__reduce_ex__',\n", - " '__repr__',\n", - " '__rmul__',\n", - " '__setattr__',\n", - " '__sizeof__',\n", - " '__str__',\n", - " '__sub__',\n", - " '__subclasshook__',\n", - " '__truediv__',\n", - " '__weakref__',\n", - " '_abc_impl',\n", - " '_apply_unitary_',\n", - " '_approx_eq_',\n", - " '_backwards_compatibility_num_qubits',\n", - " '_canonical_exponent',\n", - " '_canonical_exponent_cached',\n", - " '_circuit_diagram_info_',\n", - " '_commutes_',\n", - " '_commutes_on_qids_',\n", - " '_decompose_into_clifford_with_qubits_',\n", - " '_default_shape_from_num_qubits',\n", - " '_diagram_exponent',\n", - " '_eigen_components',\n", - " '_eigen_shifts',\n", - " '_equal_up_to_global_phase_',\n", - " '_exponent',\n", - " '_format_exponent_as_angle',\n", - " '_from_json_dict_',\n", - " '_global_shift',\n", - " '_has_stabilizer_effect_',\n", - " '_has_unitary_',\n", - " '_implemented_by_',\n", - " '_index',\n", - " '_is_parameterized_',\n", - " '_json_dict_',\n", - " '_mul_with_qubits',\n", - " '_name',\n", - " '_num_qubits_',\n", - " '_num_qubits_from_shape',\n", - " '_num_qubits_proto_from_num_qubits',\n", - " '_parameter_names_',\n", - " '_pauli_expansion_',\n", - " '_period',\n", - " '_phase_by_',\n", - " '_qasm_',\n", - " '_qid_shape_',\n", - " '_quil_',\n", - " '_resolve_parameters_',\n", - " '_rmul_with_qubits',\n", - " '_trace_distance_bound_',\n", - " '_unitary_',\n", - " '_value_equality_approximate_values_',\n", - " '_value_equality_values_',\n", - " '_value_equality_values_cls_',\n", - " '_with_exponent',\n", - " 'basis',\n", - " 'by_index',\n", - " 'by_relative_index',\n", - " 'controlled',\n", - " 'exponent',\n", - " 'global_shift',\n", - " 'in_su2',\n", - " 'num_qubits',\n", - " 'on',\n", - " 'on_each',\n", - " 'phase_exponent',\n", - " 'phased_pauli_product',\n", - " 'relative_index',\n", - " 'third',\n", - " 'validate_args',\n", - " 'with_canonical_global_phase',\n", - " 'with_probability',\n", - " 'wrap_in_linear_combination']" - ] - }, - "execution_count": 24, - "metadata": {}, - "output_type": "execute_result" + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/markdown": [ + "---" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" } ], "source": [ - "dir(cirq.X)" + "display_gates(\"XPowGate\", \"YPowGate\", \"ZPowGate\")" + ] + }, + { + "cell_type": "markdown", + "id": "110838d7-5a53-4b53-aeea-4496549e8e1f", + "metadata": {}, + "source": [ + "### More Single Qubit Gate\n", + "\n", + "Many quantum computing implementations use qubits whose energy eigenstates for a qubit that is not interacting are the computational basis states. In these cases it is often useful to move `cirq.ZPowGate`'s through other single qubit gates, \"phasing\" the " ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "id": "7a560cfb-6d7b-4837-85d1-6aa96accc264", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/markdown": [ + "---" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/markdown": [ + "#### cirq.PhasedXPowGate" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/markdown": [ + "A gate equivalent to $Z^{p} X^t Z^{-p}$.\n", + "\n", + "The unitary matrix of `cirq.PhasedXPowGate(exponent=t, phase_exponent=p)` is:\n", + "$$\n", + " \\begin{bmatrix}\n", + " e^{i \\pi t /2} \\cos(\\pi t/2) & -i e^{i \\pi (t /2 - p)} \\sin(\\pi t /2) \\\\\n", + " -i e^{i \\pi (t /2 + p)} \\sin(\\pi t /2) & e^{i \\pi t /2} \\cos(\\pi t/2)\n", + " \\end{bmatrix}\n", + "$$\n", + "\n", + "This gate is like an `cirq.XPowGate`, but which has been \"phased\",\n", + "by applying a `cirq.ZPowGate` before and after this gate. In the language\n", + "of the Bloch sphere, $p$ determines the axis in the XY plane about which\n", + "a rotation of amount determined by $t$ occurs." + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/markdown": [ + "---" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/markdown": [ + "#### cirq.PhasedXZGate" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/markdown": [ + "A single qubit gate equivalent to the circuit $Z^z Z^{a} X^x Z^{-a}$.\n", + "\n", + "The unitary matrix of `cirq.PhasedXZGate(x_exponent=x, z_exponent=z, axis_phase_exponent=a)` is:\n", + "$$\n", + " \\begin{bmatrix}\n", + " e^{i \\pi x / 2} \\cos(\\pi x /2) & -i e^{i \\pi (x/2 - a)} \\sin(\\pi x / 2) \\\\\n", + " -i e^{i \\pi (x/2 + z + a)} \\sin(\\pi x / 2) && e^{i \\pi (x / 2 + z)} \\cos(\\pi x /2)\n", + " \\end{bmatrix}\n", + "$$\n", + "\n", + "This gate can be thought of as a `cirq.PhasedXPowGate` followed by a `cirq.ZPowGate`.\n", + "\n", + "The axis phase exponent ($a$) decides which axis in the XY plane to rotate\n", + "around. The amount of rotation around that axis is decided by the x\n", + "exponent ($x$). Then the z exponent ($z$) decides how much to finally phase the qubit.\n", + "\n", + "Every single qubit gate can be written as a single `cirq.PhasedXZGate`." + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/markdown": [ + "---" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/markdown": [ + "#### cirq.HPowGate" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/markdown": [ + "A Gate that performs a rotation around the X+Z axis of the Bloch sphere.\n", + "\n", + "The unitary matrix of `cirq.HPowGate(exponent=t)` is:\n", + "$$\n", + " \\begin{bmatrix}\n", + " e^{i\\pi t/2} \\left(\\cos(\\pi t/2) - i \\frac{\\sin (\\pi t /2)}{\\sqrt{2}}\\right)\n", + " && -i e^{i\\pi t/2} \\frac{\\sin(\\pi t /2)}{\\sqrt{2}} \\\\\n", + " -i e^{i\\pi t/2} \\frac{\\sin(\\pi t /2)}{\\sqrt{2}}\n", + " && e^{i\\pi t/2} \\left(\\cos(\\pi t/2) + i \\frac{\\sin (\\pi t /2)}{\\sqrt{2}}\\right)\n", + " \\end{bmatrix}\n", + "$$\n", + "Note in particular that for $t=1$, this gives the Hadamard matrix\n", + "$$\n", + " \\begin{bmatrix}\n", + " \\frac{1}{\\sqrt{2}} & \\frac{1}{\\sqrt{2}} \\\\\n", + " \\frac{1}{\\sqrt{2}} & -\\frac{1}{\\sqrt{2}}\n", + " \\end{bmatrix}\n", + "$$\n", + "\n", + "`cirq.H`, the Hadamard gate, is an instance of this gate at `exponent=1`." + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/markdown": [ + "---" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "display_gates(\"PhasedXPowGate\", \"PhasedXZGate\", \"HPowGate\")" + ] } ], "metadata": { diff --git a/docs/qudits.ipynb b/docs/qudits.ipynb index 11c209cbea5..a453f30ac0d 100644 --- a/docs/qudits.ipynb +++ b/docs/qudits.ipynb @@ -11,7 +11,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": { "cellView": "form", "id": "nF8-mErJfgv6" @@ -64,7 +64,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": { "id": "bd9529db1c0b" }, @@ -98,7 +98,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": { "id": "6b3c6308ddd3" }, From 4bc6fb24dc568f22bcd5ec6ab1dcab0040c3d378 Mon Sep 17 00:00:00 2001 From: Dave Bacon Date: Mon, 11 Apr 2022 17:24:14 -0700 Subject: [PATCH 3/7] Fix format --- cirq-core/cirq/ops/phased_x_z_gate.py | 2 +- docs/custom_gates.ipynb | 15 +------ docs/gates.ipynb | 15 +------ docs/gatezoo.ipynb | 64 +++++++++++++++++---------- docs/qubits.ipynb | 15 +------ docs/qudits.ipynb | 15 +------ 6 files changed, 46 insertions(+), 80 deletions(-) diff --git a/cirq-core/cirq/ops/phased_x_z_gate.py b/cirq-core/cirq/ops/phased_x_z_gate.py index 9b591593e2f..030e7b59fd7 100644 --- a/cirq-core/cirq/ops/phased_x_z_gate.py +++ b/cirq-core/cirq/ops/phased_x_z_gate.py @@ -21,7 +21,7 @@ class PhasedXZGate(gate_features.SingleQubitGate): $$ \begin{bmatrix} e^{i \pi x / 2} \cos(\pi x /2) & -i e^{i \pi (x/2 - a)} \sin(\pi x / 2) \\ - -i e^{i \pi (x/2 + z + a)} \sin(\pi x / 2) && e^{i \pi (x / 2 + z)} \cos(\pi x /2) + -i e^{i \pi (x/2 + z + a)} \sin(\pi x / 2) & e^{i \pi (x / 2 + z)} \cos(\pi x /2) \end{bmatrix} $$ diff --git a/docs/custom_gates.ipynb b/docs/custom_gates.ipynb index 82d7568332c..fca153670bb 100644 --- a/docs/custom_gates.ipynb +++ b/docs/custom_gates.ipynb @@ -651,22 +651,9 @@ }, "kernelspec": { "display_name": "Python 3", - "language": "python", "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.9.9" } }, "nbformat": 4, - "nbformat_minor": 4 + "nbformat_minor": 0 } diff --git a/docs/gates.ipynb b/docs/gates.ipynb index aefe3393dbc..8bb21c4a25f 100644 --- a/docs/gates.ipynb +++ b/docs/gates.ipynb @@ -347,22 +347,9 @@ }, "kernelspec": { "display_name": "Python 3", - "language": "python", "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.9.9" } }, "nbformat": 4, - "nbformat_minor": 4 + "nbformat_minor": 0 } diff --git a/docs/gatezoo.ipynb b/docs/gatezoo.ipynb index a33f2313e43..dd558d2f194 100644 --- a/docs/gatezoo.ipynb +++ b/docs/gatezoo.ipynb @@ -57,6 +57,17 @@ "" ] }, + { + "cell_type": "markdown", + "id": "bb448db9-c75a-44e1-900c-532101085565", + "metadata": { + "id": "541571c2edcd" + }, + "source": [ + "## Setup\n", + "Note: this notebook relies on unreleased Cirq features. If you want to try these features, make sure you install cirq via `pip install cirq --pre`" + ] + }, { "cell_type": "code", "execution_count": 2, @@ -70,7 +81,7 @@ " import cirq\n", "except ImportError:\n", " print(\"installing cirq...\")\n", - " !pip install --quiet cirq\n", + " !pip install --quiet --pre cirq\n", " print(\"installed cirq.\")\n", " \n", "import IPython.display as ipd\n", @@ -90,7 +101,9 @@ { "cell_type": "markdown", "id": "5731f798-b633-4039-a138-8c70b4d8fac4", - "metadata": {}, + "metadata": { + "id": "1cd004cc2f3a" + }, "source": [ "Cirq comes with many gates that are standard across quantum computing. This notebook serves as a reference sheet for these gates.\n", "\n", @@ -106,7 +119,9 @@ "cell_type": "code", "execution_count": 3, "id": "6cb68f8a-4285-42ba-9a0f-b73bf13cd4f1", - "metadata": {}, + "metadata": { + "id": "0c3a029e2155" + }, "outputs": [ { "data": { @@ -404,7 +419,9 @@ { "cell_type": "markdown", "id": "d65a3acf-4446-4d7b-9077-39cd0b1095b6", - "metadata": {}, + "metadata": { + "id": "10c855370f45" + }, "source": [ "### Traditional Pauli Rotation Gates\n", "\n", @@ -415,7 +432,9 @@ "cell_type": "code", "execution_count": 4, "id": "e5d38c50-1774-4acf-b22c-47cd103c8a8d", - "metadata": {}, + "metadata": { + "id": "e96e1c459258" + }, "outputs": [ { "data": { @@ -578,7 +597,9 @@ { "cell_type": "markdown", "id": "9a7fec58-bbe6-4ec7-8158-5afe735ea7e2", - "metadata": {}, + "metadata": { + "id": "4bfc17ef80bb" + }, "source": [ "### Pauli PowGates\n", "\n", @@ -589,7 +610,9 @@ "cell_type": "code", "execution_count": 5, "id": "e478b06c-3a62-4b6c-a614-12dc25182304", - "metadata": {}, + "metadata": { + "id": "0e2ea8a0a0ae" + }, "outputs": [ { "data": { @@ -767,7 +790,9 @@ { "cell_type": "markdown", "id": "110838d7-5a53-4b53-aeea-4496549e8e1f", - "metadata": {}, + "metadata": { + "id": "6631a361ac42" + }, "source": [ "### More Single Qubit Gate\n", "\n", @@ -778,7 +803,9 @@ "cell_type": "code", "execution_count": 6, "id": "7a560cfb-6d7b-4837-85d1-6aa96accc264", - "metadata": {}, + "metadata": { + "id": "b5ffeefa3c76" + }, "outputs": [ { "data": { @@ -955,24 +982,15 @@ } ], "metadata": { + "colab": { + "name": "gatezoo.ipynb", + "toc_visible": true + }, "kernelspec": { "display_name": "Python 3", - "language": "python", "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.9.9" } }, "nbformat": 4, - "nbformat_minor": 5 + "nbformat_minor": 0 } diff --git a/docs/qubits.ipynb b/docs/qubits.ipynb index 1e180cd9bc2..2e93404a2b8 100644 --- a/docs/qubits.ipynb +++ b/docs/qubits.ipynb @@ -145,22 +145,9 @@ }, "kernelspec": { "display_name": "Python 3", - "language": "python", "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.9.9" } }, "nbformat": 4, - "nbformat_minor": 4 + "nbformat_minor": 0 } diff --git a/docs/qudits.ipynb b/docs/qudits.ipynb index a453f30ac0d..220384d966a 100644 --- a/docs/qudits.ipynb +++ b/docs/qudits.ipynb @@ -209,22 +209,9 @@ }, "kernelspec": { "display_name": "Python 3", - "language": "python", "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.9.9" } }, "nbformat": 4, - "nbformat_minor": 4 + "nbformat_minor": 0 } From 6854411f54559acd7076fafa1932d62961c23df2 Mon Sep 17 00:00:00 2001 From: Dave Bacon Date: Mon, 11 Apr 2022 17:30:12 -0700 Subject: [PATCH 4/7] line length --- cirq-core/cirq/ops/common_gates.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cirq-core/cirq/ops/common_gates.py b/cirq-core/cirq/ops/common_gates.py index 21ee79f52fd..bfc946629f7 100644 --- a/cirq-core/cirq/ops/common_gates.py +++ b/cirq-core/cirq/ops/common_gates.py @@ -247,7 +247,7 @@ def __repr__(self) -> str: class Rx(XPowGate): - r"""A gate, with matrix $e^{-i X t/2}$ that rotates around the X axis of the Bloch sphere by $t$. + r"""A gate with matrix $e^{-i X t/2}$ that rotates around the X axis of the Bloch sphere by $t$. The unitary matrix of `cirq.Rx(rads=t)` is: $$ From 227e5d29501864bf1a171681516b333708a32a05 Mon Sep 17 00:00:00 2001 From: Dave Bacon Date: Tue, 26 Apr 2022 04:06:39 +0000 Subject: [PATCH 5/7] review comments --- docs/gatezoo.ipynb | 824 ++------------------------------------------- docs/qudits.ipynb | 6 +- 2 files changed, 31 insertions(+), 799 deletions(-) diff --git a/docs/gatezoo.ipynb b/docs/gatezoo.ipynb index dd558d2f194..4948db5b51a 100644 --- a/docs/gatezoo.ipynb +++ b/docs/gatezoo.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "id": "2e694355-6c8f-43c5-b1ae-4e86af9fc9eb", "metadata": { "cellView": "form", @@ -43,16 +43,16 @@ "source": [ "\n", " \n", " \n", " \n", " \n", "
\n", - " View on QuantumAI\n", + " View on QuantumAI\n", " \n", - " Run in Google Colab\n", + " Run in Google Colab\n", " \n", - " View source on GitHub\n", + " View source on GitHub\n", " \n", - " Download notebook\n", + " Download notebook\n", "
" ] @@ -70,7 +70,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "id": "0b650764-128c-46fe-8b19-ee54762a77ef", "metadata": { "id": "bd9529db1c0b" @@ -117,301 +117,12 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "id": "6cb68f8a-4285-42ba-9a0f-b73bf13cd4f1", "metadata": { "id": "0c3a029e2155" }, - "outputs": [ - { - "data": { - "text/markdown": [ - "---" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/markdown": [ - "#### cirq.X" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/markdown": [ - "The Pauli X gate.\n", - "\n", - "This is the `exponent=1` instance of the `cirq.XPowGate`.\n", - "\n", - "The untary matrix of `cirq.X` is:\n", - "$$\n", - "\\begin{bmatrix}\n", - " 0 & 1 \\\\\n", - " 1 & 0\n", - "\\end{bmatrix}\n", - "$$" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/markdown": [ - "---" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/markdown": [ - "#### cirq.Y" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/markdown": [ - "The Pauli Y gate.\n", - "\n", - "This is the `exponent=1` instance of the `cirq.YPowGate`.\n", - "\n", - "The unitary matrix of `cirq.Y` is:\n", - "$$\n", - "\\begin{bmatrix}\n", - " 0 & -i \\\\\n", - " i & 0\n", - "\\end{bmatrix}\n", - "$$" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/markdown": [ - "---" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/markdown": [ - "#### cirq.Z" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/markdown": [ - "The Pauli Z gate.\n", - "\n", - "This is the `exponent=1` instance of the `cirq.ZPowGate`.\n", - "\n", - "The unitary matrix of `cirq.Z` is:\n", - "$$\n", - "\\begin{bmatrix}\n", - " 1 & 0 \\\\\n", - " 0 & -1\n", - "\\end{bmatrix}\n", - "$$" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/markdown": [ - "---" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/markdown": [ - "#### cirq.H" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/markdown": [ - "The Hadamard gate.\n", - "\n", - "The `exponent=1` instance of `cirq.HPowGate`.\n", - "\n", - "The unitary matrix of `cirq.H` is:\n", - "$$\n", - "\\begin{bmatrix}\n", - " \\frac{1}{\\sqrt{2}} & \\frac{1}{\\sqrt{2}} \\\\\n", - " \\frac{1}{\\sqrt{2}} & -\\frac{1}{\\sqrt{2}}\n", - "\\end{bmatrix}\n", - "$$" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/markdown": [ - "---" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/markdown": [ - "#### cirq.S" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/markdown": [ - "The Clifford S gate.\n", - "\n", - "The `exponent=0.5` instance of `cirq.ZPowGate`.\n", - "\n", - "The unitary matrix of `cirq.S` is:\n", - "$$\n", - "\\begin{bmatrix}\n", - " 1 & 0 \\\\\n", - " 0 & i\n", - "\\end{bmatrix}\n", - "$$" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/markdown": [ - "---" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/markdown": [ - "#### cirq.T" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/markdown": [ - "The non-Clifford T gate.\n", - "\n", - "The `exponent=0.25` instance of `cirq.ZPowGate`.\n", - "\n", - "The unitary matrix of `cirq.T` is\n", - "$$\n", - "\\begin{bmatrix}\n", - " 1 & 0 \\\\\n", - " 0 & e^{i \\pi /4}\n", - "\\end{bmatrix}\n", - "$$" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/markdown": [ - "---" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], + "outputs": [], "source": [ "display_gates(\"X\", \"Y\", \"Z\", \"H\", \"S\", \"T\")" ] @@ -430,166 +141,12 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "id": "e5d38c50-1774-4acf-b22c-47cd103c8a8d", "metadata": { "id": "e96e1c459258" }, - "outputs": [ - { - "data": { - "text/markdown": [ - "---" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/markdown": [ - "#### cirq.Rx" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/markdown": [ - "A gate, with matrix $e^{-i X t/2}$ that rotates around the X axis of the Bloch sphere by $t$.\n", - "\n", - "The unitary matrix of `cirq.Rx(rads=t)` is:\n", - "$$\n", - "e^{-i X t /2} =\n", - " \\begin{bmatrix}\n", - " \\cos(t/2) & -i \\sin(t/2) \\\\\n", - " -i \\sin(t/2) & \\cos(t/2)\n", - " \\end{bmatrix}\n", - "$$\n", - "\n", - "This gate corresponds to the traditionally defined rotation matrices about the Pauli X axis." - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/markdown": [ - "---" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/markdown": [ - "#### cirq.Ry" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/markdown": [ - "A gate with matrix $e^{-i Y t/2}$ that rotates around the Y axis of the Bloch sphere by $t$.\n", - "\n", - "The unitary matrix of `cirq.Ry(rads=t)` is:\n", - "$$\n", - "e^{-i Y t / 2} =\n", - " \\begin{bmatrix}\n", - " \\cos(t/2) & -\\sin(t/2) \\\\\n", - " \\sin(t/2) & \\cos(t/2)\n", - " \\end{bmatrix}\n", - "$$\n", - "\n", - "This gate corresponds to the traditionally defined rotation matrices about the Pauli Y axis." - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/markdown": [ - "---" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/markdown": [ - "#### cirq.Rz" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/markdown": [ - "A gate with matrix $e^{-i Z t/2}$ that rotates around the Z axis of the Bloch sphere by $t$.\n", - "\n", - "The unitary matrix of `cirq.Rz(rads=t)` is:\n", - "$$\n", - "e^{-i Z t /2} =\n", - " \\begin{bmatrix}\n", - " e^{-it/2} & 0 \\\\\n", - " 0 & e^{it/2}\n", - " \\end{bmatrix}\n", - "$$\n", - "\n", - "This gate corresponds to the traditionally defined rotation matrices about the Pauli Z axis." - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/markdown": [ - "---" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], + "outputs": [], "source": [ "display_gates(\"Rx\", \"Ry\", \"Rz\")" ] @@ -608,181 +165,12 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "id": "e478b06c-3a62-4b6c-a614-12dc25182304", "metadata": { "id": "0e2ea8a0a0ae" }, - "outputs": [ - { - "data": { - "text/markdown": [ - "---" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/markdown": [ - "#### cirq.XPowGate" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/markdown": [ - "A gate that rotates around the X axis of the Bloch sphere.\n", - "\n", - "The unitary matrix of `cirq.XPowGate(exponent=t)` is:\n", - "$$\n", - "\\begin{bmatrix}\n", - " e^{i \\pi t /2} \\cos(\\pi t) & -i e^{i \\pi t /2} \\sin(\\pi t) \\\\\n", - " -i e^{i \\pi t /2} \\sin(\\pi t) & e^{i \\pi t /2} \\cos(\\pi t)\n", - "\\end{bmatrix}\n", - "$$\n", - "\n", - "Note in particular that this gate has a global phase factor of\n", - "$e^{i \\pi t / 2}$ vs the traditionally defined rotation matrices\n", - "about the Pauli X axis. See `cirq.Rx` for rotations without the global\n", - "phase. The global phase factor can be adjusted by using the `global_shift`\n", - "parameter when initializing.\n", - "\n", - "`cirq.X`, the Pauli X gate, is an instance of this gate at `exponent=1`." - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/markdown": [ - "---" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/markdown": [ - "#### cirq.YPowGate" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/markdown": [ - "A gate that rotates around the Y axis of the Bloch sphere.\n", - "\n", - "The unitary matrix of `cirq.YPowGate(exponent=t)` is:\n", - "$$\n", - " \\begin{bmatrix}\n", - " e^{i \\pi t /2} \\cos(\\pi t /2) & - e^{i \\pi t /2} \\sin(\\pi t /2) \\\\\n", - " e^{i \\pi t /2} \\sin(\\pi t /2) & e^{i \\pi t /2} \\cos(\\pi t /2)\n", - " \\end{bmatrix}\n", - "$$\n", - "\n", - "Note in particular that this gate has a global phase factor of\n", - "$e^{i \\pi t / 2}$ vs the traditionally defined rotation matrices\n", - "about the Pauli Y axis. See `cirq.Ry` for rotations without the global\n", - "phase. The global phase factor can be adjusted by using the `global_shift`\n", - "parameter when initializing.\n", - "\n", - "`cirq.Y`, the Pauli Y gate, is an instance of this gate at `exponent=1`." - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/markdown": [ - "---" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/markdown": [ - "#### cirq.ZPowGate" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/markdown": [ - "A gate that rotates around the Z axis of the Bloch sphere.\n", - "\n", - "The unitary matrix of `cirq.ZPowGate(exponent=t)` is:\n", - "$$\n", - " \\begin{bmatrix}\n", - " 1 & 0 \\\\\n", - " 0 & e^{i \\pi t}\n", - " \\end{bmatrix}\n", - "$$\n", - "\n", - "Note in particular that this gate has a global phase factor of\n", - "$e^{i\\pi t/2}$ vs the traditionally defined rotation matrices\n", - "about the Pauli Z axis. See `cirq.Rz` for rotations without the global\n", - "phase. The global phase factor can be adjusted by using the `global_shift`\n", - "parameter when initializing.\n", - "\n", - "`cirq.Z`, the Pauli Z gate, is an instance of this gate at `exponent=1`." - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/markdown": [ - "---" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], + "outputs": [], "source": [ "display_gates(\"XPowGate\", \"YPowGate\", \"ZPowGate\")" ] @@ -801,181 +189,12 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "id": "7a560cfb-6d7b-4837-85d1-6aa96accc264", "metadata": { "id": "b5ffeefa3c76" }, - "outputs": [ - { - "data": { - "text/markdown": [ - "---" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/markdown": [ - "#### cirq.PhasedXPowGate" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/markdown": [ - "A gate equivalent to $Z^{p} X^t Z^{-p}$.\n", - "\n", - "The unitary matrix of `cirq.PhasedXPowGate(exponent=t, phase_exponent=p)` is:\n", - "$$\n", - " \\begin{bmatrix}\n", - " e^{i \\pi t /2} \\cos(\\pi t/2) & -i e^{i \\pi (t /2 - p)} \\sin(\\pi t /2) \\\\\n", - " -i e^{i \\pi (t /2 + p)} \\sin(\\pi t /2) & e^{i \\pi t /2} \\cos(\\pi t/2)\n", - " \\end{bmatrix}\n", - "$$\n", - "\n", - "This gate is like an `cirq.XPowGate`, but which has been \"phased\",\n", - "by applying a `cirq.ZPowGate` before and after this gate. In the language\n", - "of the Bloch sphere, $p$ determines the axis in the XY plane about which\n", - "a rotation of amount determined by $t$ occurs." - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/markdown": [ - "---" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/markdown": [ - "#### cirq.PhasedXZGate" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/markdown": [ - "A single qubit gate equivalent to the circuit $Z^z Z^{a} X^x Z^{-a}$.\n", - "\n", - "The unitary matrix of `cirq.PhasedXZGate(x_exponent=x, z_exponent=z, axis_phase_exponent=a)` is:\n", - "$$\n", - " \\begin{bmatrix}\n", - " e^{i \\pi x / 2} \\cos(\\pi x /2) & -i e^{i \\pi (x/2 - a)} \\sin(\\pi x / 2) \\\\\n", - " -i e^{i \\pi (x/2 + z + a)} \\sin(\\pi x / 2) && e^{i \\pi (x / 2 + z)} \\cos(\\pi x /2)\n", - " \\end{bmatrix}\n", - "$$\n", - "\n", - "This gate can be thought of as a `cirq.PhasedXPowGate` followed by a `cirq.ZPowGate`.\n", - "\n", - "The axis phase exponent ($a$) decides which axis in the XY plane to rotate\n", - "around. The amount of rotation around that axis is decided by the x\n", - "exponent ($x$). Then the z exponent ($z$) decides how much to finally phase the qubit.\n", - "\n", - "Every single qubit gate can be written as a single `cirq.PhasedXZGate`." - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/markdown": [ - "---" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/markdown": [ - "#### cirq.HPowGate" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/markdown": [ - "A Gate that performs a rotation around the X+Z axis of the Bloch sphere.\n", - "\n", - "The unitary matrix of `cirq.HPowGate(exponent=t)` is:\n", - "$$\n", - " \\begin{bmatrix}\n", - " e^{i\\pi t/2} \\left(\\cos(\\pi t/2) - i \\frac{\\sin (\\pi t /2)}{\\sqrt{2}}\\right)\n", - " && -i e^{i\\pi t/2} \\frac{\\sin(\\pi t /2)}{\\sqrt{2}} \\\\\n", - " -i e^{i\\pi t/2} \\frac{\\sin(\\pi t /2)}{\\sqrt{2}}\n", - " && e^{i\\pi t/2} \\left(\\cos(\\pi t/2) + i \\frac{\\sin (\\pi t /2)}{\\sqrt{2}}\\right)\n", - " \\end{bmatrix}\n", - "$$\n", - "Note in particular that for $t=1$, this gives the Hadamard matrix\n", - "$$\n", - " \\begin{bmatrix}\n", - " \\frac{1}{\\sqrt{2}} & \\frac{1}{\\sqrt{2}} \\\\\n", - " \\frac{1}{\\sqrt{2}} & -\\frac{1}{\\sqrt{2}}\n", - " \\end{bmatrix}\n", - "$$\n", - "\n", - "`cirq.H`, the Hadamard gate, is an instance of this gate at `exponent=1`." - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/markdown": [ - "---" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], + "outputs": [], "source": [ "display_gates(\"PhasedXPowGate\", \"PhasedXZGate\", \"HPowGate\")" ] @@ -988,9 +207,22 @@ }, "kernelspec": { "display_name": "Python 3", + "language": "python", "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.10" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/docs/qudits.ipynb b/docs/qudits.ipynb index 220384d966a..36cef899b65 100644 --- a/docs/qudits.ipynb +++ b/docs/qudits.ipynb @@ -11,7 +11,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": { "cellView": "form", "id": "nF8-mErJfgv6" @@ -64,7 +64,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": { "id": "bd9529db1c0b" }, @@ -98,7 +98,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": { "id": "6b3c6308ddd3" }, From 804dda9cd6b6bfb674371695f5c76f18eb0b4579 Mon Sep 17 00:00:00 2001 From: Dave Bacon Date: Tue, 26 Apr 2022 04:17:26 +0000 Subject: [PATCH 6/7] missing merges --- cirq-core/cirq/ops/common_gates.py | 21 +++------------------ 1 file changed, 3 insertions(+), 18 deletions(-) diff --git a/cirq-core/cirq/ops/common_gates.py b/cirq-core/cirq/ops/common_gates.py index b8143a871ec..2c2dd8eb25b 100644 --- a/cirq-core/cirq/ops/common_gates.py +++ b/cirq-core/cirq/ops/common_gates.py @@ -286,13 +286,8 @@ def _from_json_dict_(cls, rads, **kwargs) -> 'Rx': @value.value_equality -<<<<<<< HEAD -class YPowGate(eigen_gate.EigenGate, gate_features.SingleQubitGate): - r"""A gate that rotates around the Y axis of the Bloch sphere. -======= class YPowGate(eigen_gate.EigenGate): - """A gate that rotates around the Y axis of the Bloch sphere. ->>>>>>> master + r"""A gate that rotates around the Y axis of the Bloch sphere. The unitary matrix of `cirq.YPowGate(exponent=t)` is: $$ @@ -463,13 +458,8 @@ def _from_json_dict_(cls, rads, **kwargs) -> 'Ry': @value.value_equality -<<<<<<< HEAD -class ZPowGate(eigen_gate.EigenGate, gate_features.SingleQubitGate): - r"""A gate that rotates around the Z axis of the Bloch sphere. -======= class ZPowGate(eigen_gate.EigenGate): - """A gate that rotates around the Z axis of the Bloch sphere. ->>>>>>> master + r"""A gate that rotates around the Z axis of the Bloch sphere. The unitary matrix of `cirq.ZPowGate(exponent=t)` is: $$ @@ -712,13 +702,8 @@ def _from_json_dict_(cls, rads, **kwargs) -> 'Rz': return cls(rads=rads) -<<<<<<< HEAD -class HPowGate(eigen_gate.EigenGate, gate_features.SingleQubitGate): - r"""A Gate that performs a rotation around the X+Z axis of the Bloch sphere. -======= class HPowGate(eigen_gate.EigenGate): - """A Gate that performs a rotation around the X+Z axis of the Bloch sphere. ->>>>>>> master + r"""A Gate that performs a rotation around the X+Z axis of the Bloch sphere. The unitary matrix of `cirq.HPowGate(exponent=t)` is: $$ From 785332a9e6404c18b8f116a5944ad7ea5f3c308b Mon Sep 17 00:00:00 2001 From: Dave Bacon Date: Tue, 26 Apr 2022 04:19:23 +0000 Subject: [PATCH 7/7] format notebook --- docs/gatezoo.ipynb | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/docs/gatezoo.ipynb b/docs/gatezoo.ipynb index 4948db5b51a..05c7ee3345e 100644 --- a/docs/gatezoo.ipynb +++ b/docs/gatezoo.ipynb @@ -207,22 +207,9 @@ }, "kernelspec": { "display_name": "Python 3", - "language": "python", "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.9.10" } }, "nbformat": 4, - "nbformat_minor": 1 + "nbformat_minor": 0 }