#
# 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.
#
from mimiqcircuits.operations.operation import Operation
import warnings
[docs]
class Pow(Operation):
"""Raise a z-variable to a power in place: ``z[i] = z[i]^exponent``.
Acts on one z-variable. ``Pow(1)`` does nothing and emits a warning.
Args:
exponent: The exponent, a number.
Examples:
>>> from mimiqcircuits import *
>>> Pow(-1)
z[?] = z[?]^(-1)
>>> c = Circuit()
>>> c.push(Pow(2), 1)
2-zvar circuit with 1 instruction:
└── z[1] = z[1]^2
<BLANKLINE>
"""
_name = "Pow"
_num_bits = 0
_num_qubits = 0
_num_cregs = 0
_num_zvars = 1
_num_zregs = 1
_qregsizes = []
_cregsizes = []
_zregsizes = [1]
_parnames = ("exponent",)
_allow_zvar_aliasing = True
def __init__(self, exponent):
if exponent == 1:
warnings.warn("Pow(1) will be equivalent to a no-op.", stacklevel=2)
super().__init__()
self.exponent = exponent
[docs]
def inverse(self):
"""Raise :class:`TypeError`, as other non-invertible operations do.
:math:`z \\mapsto z^p` has no inverse in general: :math:`(-1)^2 = 1^2`.
"""
raise TypeError("Pow is not inversible")
[docs]
def iswrapper(self):
"""Return ``False``."""
return False
def __repr__(self):
if self.exponent >= 0:
return f"z[?] = z[?]^{self.exponent}"
return f"z[?] = z[?]^({self.exponent})"
def __str__(self):
return f"{self._name}({self.exponent})"
def format_with_targets(self, qubits, bits, zvars):
idx = zvars[0]
if self.exponent >= 0:
return f"z[{idx}] = z[{idx}]^{self.exponent}"
else:
return f"z[{idx}] = z[{idx}]^({self.exponent})"
[docs]
class Add(Operation):
"""Sum z-variables and a constant, and assign the result to the first z-variable.
The operation acts on ``N`` z-variables: the first is the destination,
the remaining ``N-1`` are the inputs. The destination is overwritten:
``z[0] = c + z[1] + ... + z[N-1]``.
To accumulate into the destination instead, pass it again as one of the
inputs (z-variable aliasing is allowed for ``Add``): for example
``c.push(Add(3), 0, 0, 1)`` evaluates ``z[0] = z[0] + z[1]``.
Args:
N (int): Number of z-variables, destination included. At least 1.
Defaults to ``1``.
c: Constant term. Defaults to ``0.0``.
Raises:
ValueError: If ``N`` is less than 1.
Examples:
>>> from mimiqcircuits import *
>>> Add(1)
z[?0] = 0.0
>>> Add(1,c=1)
z[?0] = 1
>>> Add(3,1)
z[?0] = 1 + z[?1] + z[?2]
>>> Add(4, c=3)
z[?0] = 3 + z[?1] + z[?2] + z[?3]
>>> c = Circuit()
>>> c.push(Add(2), 1, 2)
3-zvar circuit with 1 instruction:
└── z[1] = 0.0 + z[2]
<BLANKLINE>
>>> c = Circuit()
>>> c.push(Add(3), 0, 0, 1) # accumulate via aliasing
2-zvar circuit with 1 instruction:
└── z[0] = 0.0 + z[0] + z[1]
<BLANKLINE>
"""
_name = "Add"
_num_bits = 0
_num_qubits = 0
_num_qregs = 0
_num_cregs = 0
_qregsizes = []
_cregsizes = []
_parnames = ("term",)
_allow_zvar_aliasing = True
def __init__(self, N=1, c=0.0):
if N < 1:
raise ValueError("Add requires at least one z-variable.")
super().__init__()
self._num_zvars = N
self._num_zregs = 1
self._zregsizes = [N]
self.term = c
[docs]
def iswrapper(self):
"""Return ``False``."""
return False
def __repr__(self):
repr = "z[?0] ="
parts = [str(self.term)]
for i in range(1, self._num_zvars):
parts.append(f"z[?{i}]")
repr += " " + " + ".join(parts)
return repr
def __str__(self):
return f"{self._name}({self._num_zvars}, c={self.term})"
def format_with_targets(self, qubits, bits, zvars):
head = f"z[{zvars[0]}] ="
terms = [str(self.term)]
terms.extend(f"z[{z}]" for z in zvars[1:])
return f"{head} {' + '.join(terms)}"
[docs]
class Multiply(Operation):
"""Multiply z-variables and a constant, and assign the result to the first z-variable.
The operation acts on ``N`` z-variables: the first is the destination,
the remaining ``N-1`` are the inputs. The destination is overwritten:
``z[0] = c * z[1] * ... * z[N-1]``.
To accumulate into the destination instead, pass it again as one of the
inputs (z-variable aliasing is allowed for ``Multiply``): for example
``c.push(Multiply(3), 0, 0, 1)`` evaluates ``z[0] = z[0] * z[1]``.
Args:
N (int): Number of z-variables, destination included. At least 1.
Defaults to ``1``.
c: Constant factor. Defaults to ``1.0``.
Raises:
ValueError: If ``N`` is less than 1.
Examples:
>>> from mimiqcircuits import *
>>> Multiply(1)
z[?0] = 1.0
>>> Multiply(3)
z[?0] = 1.0 * z[?1] * z[?2]
>>> Multiply(3, c=2)
z[?0] = 2 * z[?1] * z[?2]
>>> Multiply(2,2)
z[?0] = 2 * z[?1]
>>> c = Circuit()
>>> c.push(Multiply(2), 1, 2)
3-zvar circuit with 1 instruction:
└── z[1] = 1.0 * z[2]
<BLANKLINE>
>>> c = Circuit()
>>> c.push(Multiply(3), 0, 0, 1) # accumulate via aliasing
2-zvar circuit with 1 instruction:
└── z[0] = 1.0 * z[0] * z[1]
<BLANKLINE>
"""
_name = "Multiply"
_num_bits = 0
_num_qubits = 0
_num_qregs = 0
_num_cregs = 0
_qregsizes = []
_cregsizes = []
_parnames = ("factor",)
_allow_zvar_aliasing = True
def __init__(self, N=1, c=1.0):
if N < 1:
raise ValueError("Multiply requires at least one z-variable.")
super().__init__()
self._num_zvars = N
self._num_zregs = 1
self._zregsizes = [N]
self.factor = c
[docs]
def iswrapper(self):
"""Return ``False``."""
return False
def __repr__(self):
repr = "z[?0] ="
parts = [str(self.factor)]
for i in range(1, self._num_zvars):
parts.append(f"z[?{i}]")
repr += " " + " * ".join(parts)
return repr
def __str__(self):
return f"{self._name}({self.num_zvars}, c={self.factor})"
def format_with_targets(self, qubits, bits, zvars):
head = f"z[{zvars[0]}] ="
terms = [str(self.factor)]
terms.extend(f"z[{z}]" for z in zvars[1:])
return f"{head} {' * '.join(terms)}"
__all__ = ["Pow", "Add", "Multiply"]