ionq_core.gates

Pure-Python unitary matrices for IonQ native trapped-ion gates.

All functions return nested tuples of complex numbers (no NumPy dependency).

Parameter conventions:

  • Phase parameters (phi, phi0, phi1) are in turns - fractions of 2*pi. So phi=0.25 means pi/2 radians.
  • Interaction parameters (angle) are in units of pi. So angle=0.25 means pi/4 radians.

Type aliases:

  • Matrix2x2 - tuple[tuple[complex, complex], tuple[complex, complex]]
  • Matrix4x4 - 4x4 nested tuple of complex numbers
Example:
from ionq_core import gpi_matrix, gpi2_matrix, ms_matrix, zz_matrix

gpi_matrix(0)  # Pauli X gate
gpi2_matrix(0.25)  # pi/2 rotation about Y axis
ms_matrix(0, 0)  # maximally-entangling MS gate
zz_matrix(0.1)  # ZZ interaction
  1# SPDX-FileCopyrightText: 2026 IonQ, Inc.
  2# SPDX-License-Identifier: Apache-2.0
  3
  4"""Pure-Python unitary matrices for IonQ native trapped-ion gates.
  5
  6All functions return nested tuples of complex numbers (no NumPy dependency).
  7
  8**Parameter conventions:**
  9
 10- Phase parameters (``phi``, ``phi0``, ``phi1``) are in **turns** -
 11  fractions of 2*pi. So ``phi=0.25`` means pi/2 radians.
 12- Interaction parameters (``angle``) are in **units of pi**. So
 13  ``angle=0.25`` means pi/4 radians.
 14
 15**Type aliases:**
 16
 17- `Matrix2x2` - ``tuple[tuple[complex, complex], tuple[complex, complex]]``
 18- `Matrix4x4` - 4x4 nested tuple of complex numbers
 19
 20Example:
 21    ```python
 22    from ionq_core import gpi_matrix, gpi2_matrix, ms_matrix, zz_matrix
 23
 24    gpi_matrix(0)  # Pauli X gate
 25    gpi2_matrix(0.25)  # pi/2 rotation about Y axis
 26    ms_matrix(0, 0)  # maximally-entangling MS gate
 27    zz_matrix(0.1)  # ZZ interaction
 28    ```
 29"""
 30
 31__all__ = ["gpi2_matrix", "gpi_matrix", "ms_matrix", "zz_matrix"]
 32
 33import cmath
 34import math
 35
 36Matrix2x2 = tuple[tuple[complex, complex], tuple[complex, complex]]
 37"""Type alias for a 2x2 unitary matrix (single-qubit gate)."""
 38
 39Matrix4x4 = tuple[
 40    tuple[complex, complex, complex, complex],
 41    tuple[complex, complex, complex, complex],
 42    tuple[complex, complex, complex, complex],
 43    tuple[complex, complex, complex, complex],
 44]
 45"""Type alias for a 4x4 unitary matrix (two-qubit gate)."""
 46
 47_2PI = 2 * math.pi
 48
 49
 50def gpi_matrix(phi: float) -> Matrix2x2:
 51    r"""Single-qubit GPI gate.
 52
 53    Matrix form: ``[[0, e^{-i*2*pi*phi}], [e^{i*2*pi*phi}, 0]]``
 54
 55    At ``phi=0`` this is the Pauli X gate.
 56
 57    Args:
 58        phi: Phase angle in turns (fractions of 2*pi).
 59
 60    Returns:
 61        A `Matrix2x2` unitary matrix.
 62
 63    Examples:
 64        ```python
 65        >>> gpi_matrix(0)       # Pauli X
 66        ((0, (1+0j)), ((1+0j), 0))
 67        ```
 68    """
 69    e = cmath.exp(1j * _2PI * phi)
 70    return ((0, 1 / e), (e, 0))
 71
 72
 73def gpi2_matrix(phi: float) -> Matrix2x2:
 74    """Single-qubit GPI2 gate (pi/2 rotation about an axis in the XY plane).
 75
 76    Args:
 77        phi: Phase angle in turns (fractions of 2*pi) defining the
 78            rotation axis in the XY plane.
 79
 80    Returns:
 81        A `Matrix2x2` unitary matrix.
 82    """
 83    e = cmath.exp(1j * _2PI * phi)
 84    s = 1 / math.sqrt(2)
 85    return ((s, -1j * s / e), (-1j * s * e, s))
 86
 87
 88def ms_matrix(phi0: float, phi1: float, angle: float = 0.25) -> Matrix4x4:
 89    """Two-qubit Molmer-Sorensen (MS) gate.
 90
 91    The default ``angle=0.25`` produces a maximally-entangling gate.
 92
 93    Args:
 94        phi0: Frame rotation phase for qubit 0 in turns.
 95        phi1: Frame rotation phase for qubit 1 in turns.
 96        angle: Interaction angle in units of pi. Defaults to 0.25
 97            (i.e. pi/4 radians).
 98
 99    Returns:
100        A `Matrix4x4` unitary matrix.
101
102    Examples:
103        ```python
104        >>> ms_matrix(0, 0)         # maximally-entangling MS gate
105        >>> ms_matrix(0, 0, 0.125)  # partial entanglement
106        ```
107    """
108    a = math.pi * angle
109    ca, sa = math.cos(a), math.sin(a)
110    ep = cmath.exp(1j * _2PI * (phi0 + phi1))
111    em = cmath.exp(1j * _2PI * (phi0 - phi1))
112    return (
113        (ca, 0, 0, -1j * sa / ep),
114        (0, ca, -1j * sa / em, 0),
115        (0, -1j * sa * em, ca, 0),
116        (-1j * sa * ep, 0, 0, ca),
117    )
118
119
120def zz_matrix(angle: float) -> Matrix4x4:
121    """Two-qubit ZZ interaction gate.
122
123    Diagonal matrix: ``diag(e^{-i*pi*a}, e^{i*pi*a}, e^{i*pi*a}, e^{-i*pi*a})``
124
125    At ``angle=0`` this is the identity gate.
126
127    Args:
128        angle: Interaction angle in units of pi.
129
130    Returns:
131        A `Matrix4x4` unitary matrix.
132    """
133    em = cmath.exp(-1j * math.pi * angle)
134    ep = cmath.exp(1j * math.pi * angle)
135    return (
136        (em, 0, 0, 0),
137        (0, ep, 0, 0),
138        (0, 0, ep, 0),
139        (0, 0, 0, em),
140    )
def gpi2_matrix(phi: float) -> tuple[tuple[complex, complex], tuple[complex, complex]]:
74def gpi2_matrix(phi: float) -> Matrix2x2:
75    """Single-qubit GPI2 gate (pi/2 rotation about an axis in the XY plane).
76
77    Args:
78        phi: Phase angle in turns (fractions of 2*pi) defining the
79            rotation axis in the XY plane.
80
81    Returns:
82        A `Matrix2x2` unitary matrix.
83    """
84    e = cmath.exp(1j * _2PI * phi)
85    s = 1 / math.sqrt(2)
86    return ((s, -1j * s / e), (-1j * s * e, s))

Single-qubit GPI2 gate (pi/2 rotation about an axis in the XY plane).

Arguments:
  • phi: Phase angle in turns (fractions of 2*pi) defining the rotation axis in the XY plane.
Returns:

A Matrix2x2 unitary matrix.

def gpi_matrix(phi: float) -> tuple[tuple[complex, complex], tuple[complex, complex]]:
51def gpi_matrix(phi: float) -> Matrix2x2:
52    r"""Single-qubit GPI gate.
53
54    Matrix form: ``[[0, e^{-i*2*pi*phi}], [e^{i*2*pi*phi}, 0]]``
55
56    At ``phi=0`` this is the Pauli X gate.
57
58    Args:
59        phi: Phase angle in turns (fractions of 2*pi).
60
61    Returns:
62        A `Matrix2x2` unitary matrix.
63
64    Examples:
65        ```python
66        >>> gpi_matrix(0)       # Pauli X
67        ((0, (1+0j)), ((1+0j), 0))
68        ```
69    """
70    e = cmath.exp(1j * _2PI * phi)
71    return ((0, 1 / e), (e, 0))

Single-qubit GPI gate.

Matrix form: [[0, e^{-i*2*pi*phi}], [e^{i*2*pi*phi}, 0]]

At phi=0 this is the Pauli X gate.

Arguments:
  • phi: Phase angle in turns (fractions of 2*pi).
Returns:

A Matrix2x2 unitary matrix.

Examples:
>>> gpi_matrix(0)       # Pauli X
((0, (1+0j)), ((1+0j), 0))
def ms_matrix( phi0: float, phi1: float, angle: float = 0.25) -> tuple[tuple[complex, complex, complex, complex], tuple[complex, complex, complex, complex], tuple[complex, complex, complex, complex], tuple[complex, complex, complex, complex]]:
 89def ms_matrix(phi0: float, phi1: float, angle: float = 0.25) -> Matrix4x4:
 90    """Two-qubit Molmer-Sorensen (MS) gate.
 91
 92    The default ``angle=0.25`` produces a maximally-entangling gate.
 93
 94    Args:
 95        phi0: Frame rotation phase for qubit 0 in turns.
 96        phi1: Frame rotation phase for qubit 1 in turns.
 97        angle: Interaction angle in units of pi. Defaults to 0.25
 98            (i.e. pi/4 radians).
 99
100    Returns:
101        A `Matrix4x4` unitary matrix.
102
103    Examples:
104        ```python
105        >>> ms_matrix(0, 0)         # maximally-entangling MS gate
106        >>> ms_matrix(0, 0, 0.125)  # partial entanglement
107        ```
108    """
109    a = math.pi * angle
110    ca, sa = math.cos(a), math.sin(a)
111    ep = cmath.exp(1j * _2PI * (phi0 + phi1))
112    em = cmath.exp(1j * _2PI * (phi0 - phi1))
113    return (
114        (ca, 0, 0, -1j * sa / ep),
115        (0, ca, -1j * sa / em, 0),
116        (0, -1j * sa * em, ca, 0),
117        (-1j * sa * ep, 0, 0, ca),
118    )

Two-qubit Molmer-Sorensen (MS) gate.

The default angle=0.25 produces a maximally-entangling gate.

Arguments:
  • phi0: Frame rotation phase for qubit 0 in turns.
  • phi1: Frame rotation phase for qubit 1 in turns.
  • angle: Interaction angle in units of pi. Defaults to 0.25 (i.e. pi/4 radians).
Returns:

A Matrix4x4 unitary matrix.

Examples:
>>> ms_matrix(0, 0)         # maximally-entangling MS gate
>>> ms_matrix(0, 0, 0.125)  # partial entanglement
def zz_matrix( angle: float) -> tuple[tuple[complex, complex, complex, complex], tuple[complex, complex, complex, complex], tuple[complex, complex, complex, complex], tuple[complex, complex, complex, complex]]:
121def zz_matrix(angle: float) -> Matrix4x4:
122    """Two-qubit ZZ interaction gate.
123
124    Diagonal matrix: ``diag(e^{-i*pi*a}, e^{i*pi*a}, e^{i*pi*a}, e^{-i*pi*a})``
125
126    At ``angle=0`` this is the identity gate.
127
128    Args:
129        angle: Interaction angle in units of pi.
130
131    Returns:
132        A `Matrix4x4` unitary matrix.
133    """
134    em = cmath.exp(-1j * math.pi * angle)
135    ep = cmath.exp(1j * math.pi * angle)
136    return (
137        (em, 0, 0, 0),
138        (0, ep, 0, 0),
139        (0, 0, ep, 0),
140        (0, 0, 0, em),
141    )

Two-qubit ZZ interaction gate.

Diagonal matrix: diag(e^{-i*pi*a}, e^{i*pi*a}, e^{i*pi*a}, e^{-i*pi*a})

At angle=0 this is the identity gate.

Arguments:
  • angle: Interaction angle in units of pi.
Returns:

A Matrix4x4 unitary matrix.