#
# Copyright © 2022-2023 University of Strasbourg. All Rights Reserved.
# Copyright © 2023-2025 QPerfect. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import symengine as se
from mimiqcircuits.operations.operator import AbstractOperator
def to_subscript(number):
subscript_map = str.maketrans("0123456789", "₀₁₂₃₄₅₆₇₈₉")
return str(number).translate(subscript_map)
[docs]
class Projector0(AbstractOperator):
r"""One-qubit operator corresponding to a projection onto :math:`|0\rangle`.
Matrix Representation:
.. math::
\begin{pmatrix}
a & 0 \\
0 & 0
\end{pmatrix}
The factor ``a`` scales the projector, for example to add a phase. The
operator is not unitary: use it in an :class:`ExpectationValue` or as a
Kraus operator. Equivalent to ``DiagonalOp(a, 0)``.
See Also:
:class:`Projector1`, :class:`DiagonalOp`
Args:
a (complex): The top-left entry of the matrix. Defaults to 1.
Examples:
>>> from mimiqcircuits import *
>>> Projector0()
P₀(1)
>>> Projector0(0.5)
P₀(0.5)
>>> c = Circuit()
>>> c.push(ExpectationValue(Projector0()), 1, 1)
2-qubit, 2-zvar circuit with 1 instruction:
└── ⟨P₀(1)⟩ @ q[1], z[1]
<BLANKLINE>
"""
_name = "Projector0"
_num_qubits = 1
_parnames = ()
_qregsizes = [1]
def __init__(self, a=1):
self.a = a
super().__init__()
self._parnames = ("a",)
def _matrix(self):
return se.Matrix([[self.a, 0], [0, 0]])
@property
def parnames(self):
return self._parnames
def __str__(self):
return f"P{to_subscript(0)}({self.a})"
[docs]
def opsquared(self):
r"""Return :math:`O^\dagger O`, the same projector scaled by :math:`|a|^2`."""
return Projector0(abs(self.a) ** 2)
[docs]
class Projector1(AbstractOperator):
r"""One-qubit operator corresponding to a projection onto :math:`|1\rangle`.
Matrix Representation:
.. math::
\begin{pmatrix}
0 & 0 \\
0 & a
\end{pmatrix}
The factor ``a`` scales the projector, for example to add a phase. The
operator is not unitary: use it in an :class:`ExpectationValue` or as a
Kraus operator. Equivalent to ``DiagonalOp(0, a)``.
See Also:
:class:`Projector0`, :class:`DiagonalOp`
Args:
a (complex): The bottom-right entry of the matrix. Defaults to 1.
Examples:
>>> from mimiqcircuits import *
>>> Projector1()
P₁(1)
>>> Projector1(0.5)
P₁(0.5)
>>> c = Circuit()
>>> c.push(ExpectationValue(Projector1()), 1, 1)
2-qubit, 2-zvar circuit with 1 instruction:
└── ⟨P₁(1)⟩ @ q[1], z[1]
<BLANKLINE>
"""
_name = "Projector1"
_num_qubits = 1
_parnames = ()
_qregsizes = [1]
def __init__(self, a=1):
self.a = a
super().__init__()
self._parnames = ("a",)
def _matrix(self):
return se.Matrix([[0, 0], [0, self.a]])
@property
def parnames(self):
return self._parnames
def __str__(self):
return f"P{to_subscript(1)}({self.a})"
[docs]
def opsquared(self):
r"""Return :math:`O^\dagger O`, the same projector scaled by :math:`|a|^2`."""
return Projector1(abs(self.a) ** 2)
[docs]
class ProjectorX0(AbstractOperator):
r"""One-qubit operator corresponding to a projection onto :math:`|+\rangle`.
Matrix Representation:
.. math::
\frac{a}{2}
\begin{pmatrix}
1 & 1 \\
1 & 1
\end{pmatrix}
The factor ``a`` scales the projector, for example to add a phase. The
operator is not unitary: use it in an :class:`ExpectationValue` or as a
Kraus operator.
See Also:
:class:`ProjectorX1`
Args:
a (complex): Scaling factor for the matrix. Defaults to 1.
Examples:
>>> from mimiqcircuits import *
>>> ProjectorX0()
PX₀(1)
>>> ProjectorX0(0.5)
PX₀(0.5)
>>> c = Circuit()
>>> c.push(ExpectationValue(ProjectorX0()), 1, 1)
2-qubit, 2-zvar circuit with 1 instruction:
└── ⟨PX₀(1)⟩ @ q[1], z[1]
<BLANKLINE>
"""
_name = "PX0"
_num_qubits = 1
_parnames = ()
_qregsizes = [1]
def __init__(self, a=1):
self.a = a
super().__init__()
self._parnames = ("a",)
def _matrix(self):
return self.a / 2 * se.Matrix([[1, 1], [1, 1]])
@property
def parnames(self):
return self._parnames
def __str__(self):
return f"PX{to_subscript(0)}({self.a})"
[docs]
def opsquared(self):
r"""Return :math:`O^\dagger O`, the same projector scaled by :math:`|a|^2`."""
return ProjectorX0(abs(self.a) ** 2)
[docs]
class ProjectorX1(AbstractOperator):
r"""One-qubit operator corresponding to a projection onto :math:`|-\rangle`.
Matrix Representation:
.. math::
\frac{a}{2}
\begin{pmatrix}
1 & -1 \\
-1 & 1
\end{pmatrix}
The factor ``a`` scales the projector, for example to add a phase. The
operator is not unitary: use it in an :class:`ExpectationValue` or as a
Kraus operator.
See Also:
:class:`ProjectorX0`
Args:
a (complex): Scaling factor for the matrix. Defaults to 1.
Examples:
>>> from mimiqcircuits import *
>>> ProjectorX1()
PX₁(1)
>>> ProjectorX1(0.5)
PX₁(0.5)
>>> c = Circuit()
>>> c.push(ExpectationValue(ProjectorX1()), 1, 1)
2-qubit, 2-zvar circuit with 1 instruction:
└── ⟨PX₁(1)⟩ @ q[1], z[1]
<BLANKLINE>
"""
_name = "PX1"
_num_qubits = 1
_parnames = ()
_qregsizes = [1]
def __init__(self, a=1):
self.a = a
super().__init__()
self._parnames = ("a",)
def _matrix(self):
return self.a / 2 * se.Matrix([[1, -1], [-1, 1]])
@property
def parnames(self):
return self._parnames
def __str__(self):
return f"PX{to_subscript(1)}({self.a})"
[docs]
def opsquared(self):
r"""Return :math:`O^\dagger O`, the same projector scaled by :math:`|a|^2`."""
return ProjectorX1(abs(self.a) ** 2)
[docs]
class ProjectorY0(AbstractOperator):
r"""One-qubit operator corresponding to a projection onto :math:`|{+i}\rangle`.
Matrix Representation:
.. math::
\frac{a}{2}
\begin{pmatrix}
1 & -i \\
i & 1
\end{pmatrix}
The factor ``a`` scales the projector, for example to add a phase. The
operator is not unitary: use it in an :class:`ExpectationValue` or as a
Kraus operator.
See Also:
:class:`ProjectorY1`
Args:
a (complex): Scaling factor for the matrix. Defaults to 1.
Examples:
>>> from mimiqcircuits import *
>>> ProjectorY0()
PY₀(1)
>>> ProjectorY0(0.5)
PY₀(0.5)
>>> c = Circuit()
>>> c.push(ExpectationValue(ProjectorY0()), 1, 1)
2-qubit, 2-zvar circuit with 1 instruction:
└── ⟨PY₀(1)⟩ @ q[1], z[1]
<BLANKLINE>
"""
_name = "PY0"
_num_qubits = 1
_parnames = ()
_qregsizes = [1]
def __init__(self, a=1):
self.a = a
super().__init__()
self._parnames = ("a",)
def _matrix(self):
return self.a / 2 * se.Matrix([[1, -1j], [1j, 1]])
@property
def parnames(self):
return self._parnames
def __str__(self):
return f"PY{to_subscript(0)}({self.a})"
[docs]
def opsquared(self):
r"""Return :math:`O^\dagger O`, the same projector scaled by :math:`|a|^2`."""
return ProjectorY0(abs(self.a) ** 2)
[docs]
class ProjectorY1(AbstractOperator):
r"""One-qubit operator corresponding to a projection onto :math:`|{-i}\rangle`.
Matrix Representation:
.. math::
\frac{a}{2}
\begin{pmatrix}
1 & i \\
-i & 1
\end{pmatrix}
The factor ``a`` scales the projector, for example to add a phase. The
operator is not unitary: use it in an :class:`ExpectationValue` or as a
Kraus operator.
See Also:
:class:`ProjectorY0`
Args:
a (complex): Scaling factor for the matrix. Defaults to 1.
Examples:
>>> from mimiqcircuits import *
>>> ProjectorY1()
PY₁(1)
>>> ProjectorY1(0.5)
PY₁(0.5)
>>> c = Circuit()
>>> c.push(ExpectationValue(ProjectorY1()), 1, 1)
2-qubit, 2-zvar circuit with 1 instruction:
└── ⟨PY₁(1)⟩ @ q[1], z[1]
<BLANKLINE>
"""
_name = "PY1"
_num_qubits = 1
_parnames = ()
_qregsizes = [1]
def __init__(self, a=1):
self.a = a
super().__init__()
self._parnames = ("a",)
def _matrix(self):
return self.a / 2 * se.Matrix([[1, 1j], [-1j, 1]])
@property
def parnames(self):
return self._parnames
def __str__(self):
return f"PY{to_subscript(1)}({self.a})"
[docs]
def opsquared(self):
r"""Return :math:`O^\dagger O`, the same projector scaled by :math:`|a|^2`."""
return ProjectorY1(abs(self.a) ** 2)
[docs]
class Projector00(AbstractOperator):
r"""Two-qubit operator corresponding to a projection onto :math:`|00\rangle`.
Matrix Representation:
.. math::
\begin{pmatrix}
a & 0 & 0 & 0\\
0 & 0 & 0 & 0\\
0 & 0 & 0 & 0\\
0 & 0 & 0 & 0
\end{pmatrix}
The factor ``a`` scales the projector, for example to add a phase. The
operator is not unitary: use it in an :class:`ExpectationValue` or as a
Kraus operator.
See Also:
:class:`Projector01`, :class:`Projector10`, :class:`Projector11`
Args:
a (complex): Scaling factor for the matrix. Defaults to 1.
Examples:
>>> from mimiqcircuits import *
>>> Projector00()
P₀₀(1)
>>> Projector00(0.5)
P₀₀(0.5)
>>> c = Circuit()
>>> c.push(ExpectationValue(Projector00()), 1, 2, 1)
3-qubit, 2-zvar circuit with 1 instruction:
└── ⟨P₀₀(1)⟩ @ q[1:2], z[1]
<BLANKLINE>
"""
_name = "Projector00"
_num_qubits = 2
_parnames = ()
_qregsizes = [2]
def __init__(self, a=1):
self.a = a
super().__init__()
self._parnames = ("a",)
def _matrix(self):
return se.Matrix([[self.a, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]])
@property
def parnames(self):
return self._parnames
def __str__(self):
return f"P{to_subscript(0)}{to_subscript(0)}({self.a})"
[docs]
def opsquared(self):
r"""Return :math:`O^\dagger O`, the same projector scaled by :math:`|a|^2`."""
return Projector00(abs(self.a) ** 2)
[docs]
class Projector01(AbstractOperator):
r"""Two-qubit operator corresponding to a projection onto :math:`|01\rangle`.
Matrix Representation:
.. math::
\begin{pmatrix}
0 & 0 & 0 & 0\\
0 & a & 0 & 0\\
0 & 0 & 0 & 0\\
0 & 0 & 0 & 0
\end{pmatrix}
The factor ``a`` scales the projector, for example to add a phase. The
operator is not unitary: use it in an :class:`ExpectationValue` or as a
Kraus operator.
See Also:
:class:`Projector00`, :class:`Projector10`, :class:`Projector11`
Args:
a (complex): Scaling factor for the matrix. Defaults to 1.
Examples:
>>> from mimiqcircuits import *
>>> Projector01()
P₀₁(1)
>>> Projector01(0.5)
P₀₁(0.5)
>>> c = Circuit()
>>> c.push(ExpectationValue(Projector01()), 1, 2, 1)
3-qubit, 2-zvar circuit with 1 instruction:
└── ⟨P₀₁(1)⟩ @ q[1:2], z[1]
<BLANKLINE>
"""
_name = "Projector01"
_num_qubits = 2
_parnames = ()
_qregsizes = [2]
def __init__(self, a=1):
self.a = a
super().__init__()
self._parnames = ("a",)
def _matrix(self):
return se.Matrix([[0, 0, 0, 0], [0, self.a, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]])
@property
def parnames(self):
return self._parnames
def __str__(self):
return f"P{to_subscript(0)}{to_subscript(1)}({self.a})"
[docs]
def opsquared(self):
r"""Return :math:`O^\dagger O`, the same projector scaled by :math:`|a|^2`."""
return Projector01(abs(self.a) ** 2)
[docs]
class Projector10(AbstractOperator):
r"""Two-qubit operator corresponding to a projection onto :math:`|10\rangle`.
Matrix Representation:
.. math::
\begin{pmatrix}
0 & 0 & 0 & 0\\
0 & 0 & 0 & 0\\
0 & 0 & a & 0\\
0 & 0 & 0 & 0
\end{pmatrix}
The factor ``a`` scales the projector, for example to add a phase. The
operator is not unitary: use it in an :class:`ExpectationValue` or as a
Kraus operator.
See Also:
:class:`Projector00`, :class:`Projector01`, :class:`Projector11`
Args:
a (complex): Scaling factor for the matrix. Defaults to 1.
Examples:
>>> from mimiqcircuits import *
>>> Projector10()
P₁₀(1)
>>> Projector10(0.5)
P₁₀(0.5)
>>> c = Circuit()
>>> c.push(ExpectationValue(Projector10()), 1, 2, 1)
3-qubit, 2-zvar circuit with 1 instruction:
└── ⟨P₁₀(1)⟩ @ q[1:2], z[1]
<BLANKLINE>
"""
_name = "Projector10"
_num_qubits = 2
_parnames = ()
_qregsizes = [2]
def __init__(self, a=1):
self.a = a
super().__init__()
self._parnames = ("a",)
def _matrix(self):
return se.Matrix([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, self.a, 0], [0, 0, 0, 0]])
@property
def parnames(self):
return self._parnames
def __str__(self):
return f"P{to_subscript(1)}{to_subscript(0)}({self.a})"
[docs]
def opsquared(self):
r"""Return :math:`O^\dagger O`, the same projector scaled by :math:`|a|^2`."""
return Projector10(abs(self.a) ** 2)
[docs]
class Projector11(AbstractOperator):
r"""Two-qubit operator corresponding to a projection onto :math:`|11\rangle`.
Matrix Representation:
.. math::
\begin{pmatrix}
0 & 0 & 0 & 0\\
0 & 0 & 0 & 0\\
0 & 0 & 0 & 0\\
0 & 0 & 0 & a
\end{pmatrix}
The factor ``a`` scales the projector, for example to add a phase. The
operator is not unitary: use it in an :class:`ExpectationValue` or as a
Kraus operator.
See Also:
:class:`Projector00`, :class:`Projector01`, :class:`Projector10`
Args:
a (complex): Scaling factor for the matrix. Defaults to 1.
Examples:
>>> from mimiqcircuits import *
>>> Projector11()
P₁₁(1)
>>> Projector11(0.5)
P₁₁(0.5)
>>> c = Circuit()
>>> c.push(ExpectationValue(Projector11()), 1, 2, 1)
3-qubit, 2-zvar circuit with 1 instruction:
└── ⟨P₁₁(1)⟩ @ q[1:2], z[1]
<BLANKLINE>
"""
_name = "Projector11"
_num_qubits = 2
_parnames = ()
_qregsizes = [2]
def __init__(self, a=1):
self.a = a
super().__init__()
self._parnames = ("a",)
def _matrix(self):
return se.Matrix([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, self.a]])
@property
def parnames(self):
return self._parnames
def __str__(self):
return f"P{to_subscript(1)}{to_subscript(1)}({self.a})"
[docs]
def opsquared(self):
r"""Return :math:`O^\dagger O`, the same projector scaled by :math:`|a|^2`."""
return Projector11(abs(self.a) ** 2)
[docs]
class ProjectorZ0(AbstractOperator):
r"""Alias of :class:`Projector0`.
``ProjectorZ0()`` takes no arguments and returns ``Projector0()``.
Examples:
>>> from mimiqcircuits import *
>>> ProjectorZ0()
P₀(1)
"""
def __new__(self):
return Projector0()
[docs]
class ProjectorZ1(AbstractOperator):
r"""Alias of :class:`Projector1`.
``ProjectorZ1()`` takes no arguments and returns ``Projector1()``.
Examples:
>>> from mimiqcircuits import *
>>> ProjectorZ1()
P₁(1)
"""
def __new__(self):
return Projector1()
__all__ = [
"Projector0",
"Projector00",
"Projector01",
"Projector1",
"Projector10",
"Projector11",
"ProjectorY1",
"ProjectorY0",
"ProjectorX1",
"ProjectorX0",
"ProjectorZ1",
"ProjectorZ0",
]