# Copyright 2018 Carsten Blank
#
# 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 math
from typing import Tuple, List, Union
from qiskit import QuantumRegister, QuantumCircuit
from qiskit.circuit import Gate, Instruction, Qubit, Clbit
from qiskit.extensions import HGate, CU1Gate
[docs]def get_theta(k):
# type: (int) -> float
r"""
As the conditional rotations are defined by a parameter k we have a
convenience function for this:
.. math::
\theta = \pm \frac{2\pi}{2^{(|k|)}}
:param k: the integer for the angle
:return: the angle
"""
sign = -1 if k < 0 else 1
lam = sign * 2 * math.pi * 2**(-abs(k))
return lam
[docs]def qft(self, q):
# type: (QuantumCircuit, Union[List[Qubit], QuantumRegister]) -> Instruction
"""
Applies the Quantum Fourier Transform to q
:param self: the circuit to which the qft is applied
:param q: the quantum register or list of quantum register/index tuples
:return: the circuit with applied qft
"""
return self.append(QuantumFourierTransformGate(len(q)), [q])
[docs]def qft_dg(self, q):
# type: (QuantumCircuit, Union[List[Qubit], QuantumRegister]) -> Instruction
"""
Applies the inverse Quantum Fourier Transform to q
:param self: the circuit to which the qft_dag is applied
:param q: the quantum register or list of quantum register/index tuples
:return: the circuit with applied qft_dag
"""
return qft(self, q).inverse()