Skip to content

gkp

Cat Code Qubit

GKPQubit

Bases: BosonicQubit

GKP Qubit Class.

Source code in jaxquantum/codes/gkp.py
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
class GKPQubit(BosonicQubit):
    """
    GKP Qubit Class.
    """

    name = "gkp"

    def _params_validation(self):
        super()._params_validation()

        if "delta" not in self.params:
            self.params["delta"] = 0.25
        self.params["l"] = 2.0 * jnp.sqrt(jnp.pi)
        s_delta = jnp.sinh(self.params["delta"] ** 2)
        self.params["epsilon"] = s_delta * self.params["l"]

    def _gen_common_gates(self) -> None:
        """
        Overriding this method to add additional common gates.
        """
        super()._gen_common_gates()

        # phase space
        self.common_gates["x"] = (
            self.common_gates["a_dag"] + self.common_gates["a"]
        ) / jnp.sqrt(2.0)
        self.common_gates["p"] = (
            1.0j * (self.common_gates["a_dag"] - self.common_gates["a"]) / jnp.sqrt(2.0)
        )

        # finite energy
        self.common_gates["E"] = jqt.expm(
            -(self.params["delta"] ** 2)
            * self.common_gates["a_dag"]
            @ self.common_gates["a"]
        )
        self.common_gates["E_inv"] = jqt.expm(
            self.params["delta"] ** 2
            * self.common_gates["a_dag"]
            @ self.common_gates["a"]
        )

        # axis
        x_axis, z_axis = self._get_axis()
        y_axis = x_axis + z_axis

        # gates
        X_0 = jqt.expm(1.0j * self.params["l"] / 2.0 * z_axis)
        Z_0 = jqt.expm(1.0j * self.params["l"] / 2.0 * x_axis)
        Y_0 = 1.0j * X_0 @ Z_0
        self.common_gates["X_0"] = X_0
        self.common_gates["Z_0"] = Z_0
        self.common_gates["Y_0"] = Y_0
        self.common_gates["X"] = self._make_op_finite_energy(X_0)
        self.common_gates["Z"] = self._make_op_finite_energy(Z_0)
        self.common_gates["Y"] = self._make_op_finite_energy(Y_0)

        # symmetric stabilizers and gates
        self.common_gates["Z_s_0"] = self._symmetrized_expm(
            1.0j * self.params["l"] / 2.0 * x_axis
        )
        self.common_gates["S_x_0"] = self._symmetrized_expm(
            1.0j * self.params["l"] * z_axis
        )
        self.common_gates["S_z_0"] = self._symmetrized_expm(
            1.0j * self.params["l"] * x_axis
        )
        self.common_gates["S_y_0"] = self._symmetrized_expm(
            1.0j * self.params["l"] * y_axis
        )

    @staticmethod
    def _q_quadrature(q_points, n):
        q_points = q_points.T

        F_0_init = jnp.ones_like(q_points)
        F_1_init = jnp.sqrt(2) * q_points

        def scan_body(n, carry):
            F_0, F_1 = carry
            F_n = (jnp.sqrt(2 / n) * lax.mul(q_points, F_1) - jnp.sqrt(
                (n - 1) / n) * F_0)

            new_carry = (F_1, F_n)

            return new_carry

        initial_carry = (F_0_init, F_1_init)
        final_carry = lax.fori_loop(2, jnp.max(jnp.array([n + 1, 2])),
                                    scan_body, initial_carry)

        q_quad = lax.select(n == 0, F_0_init,
                            lax.select(n == 1, F_1_init,
                                       final_carry[1]))

        q_quad = jnp.pi ** (-0.25) * lax.mul(
            jnp.exp(-lax.pow(q_points, 2) / 2), q_quad)

        return q_quad

    @staticmethod
    def _compute_gkp_basis_z(delta, dim, mu, series_trunc=100):
        """
        Args:
            mu: state index (0 or 1)

        Returns:
            GKP basis state

        Adapted from code by Lev-Arcady Sellem <lev-arcady.sellem@inria.fr>
        """

        # We choose the truncation of our series summation such that we
        # capture 6 sigmas of the envelope for a value of delta of 0.02.
        # delta * (truncat_series*2*sqrt(pi)) = 6


        q_points = jnp.sqrt(jnp.pi) * (2 * jnp.arange(series_trunc) + mu)

        def compute_pop(n):
            quadvals = GKPQubit._q_quadrature(q_points, n)
            return jnp.exp(-(delta ** 2) * n) * (
                    2 * jnp.sum(quadvals) - (1 - mu) * quadvals[0])

        psi_even = vmap(compute_pop)(jnp.arange(0, dim, 2))

        psi = jnp.zeros(2 * psi_even.size, dtype=psi_even.dtype)

        psi = psi.at[::2].set(psi_even)

        psi = jqt.Qarray.create(jnp.array(psi)[:dim])

        return psi.unit()




    def _get_basis_z(self) -> Tuple[jqt.Qarray, jqt.Qarray]:
        """
        Construct basis states |+-z>.
        """

        delta = self.params["delta"]
        dim = self.params["N"]

        if delta<0.02:
            warnings.warn("State preparation with delta values lower than 0.02 might lead to loss of accuracy.")

        jitted_compute_gkp_basis_z = jit(self._compute_gkp_basis_z, 
                                         static_argnames=("dim",))

        plus_z = jitted_compute_gkp_basis_z(delta, dim, 0)
        minus_z = jitted_compute_gkp_basis_z(delta, dim, 1)

        return plus_z, minus_z

    # utils
    # ======================================================
    def _get_axis(self):
        x_axis = self.common_gates["x"]
        z_axis = -self.common_gates["p"]
        return x_axis, z_axis

    def _make_op_finite_energy(self, op):
        return self.common_gates["E"] @ op @ self.common_gates["E_inv"]

    def _symmetrized_expm(self, op):
        return (jqt.expm(op) + jqt.expm(-1.0 * op)) / 2.0

    # gates
    # ======================================================
    @property
    def x_U(self) -> jqt.Qarray:
        return self.common_gates["X"]

    @property
    def y_U(self) -> jqt.Qarray:
        return self.common_gates["Y"]

    @property
    def z_U(self) -> jqt.Qarray:
        return self.common_gates["Z"]