Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def convert_one_qubit_gate(self, gate, typeinfo) :
glist = list()
if typeinfo[1] == 0 :
# no control bit
assert isinstance(gate, bqgate.OneQubitGate)
one_qubit_gates = self.create_gates(typeinfo[0], gate)
# "tdg": gate.TDagGate, "sdg": gate.SDagGate,
if gate.lowername[1:] == 'dg' :
for qg in one_qubit_gates :
qg.set_adjoint(True)
glist += one_qubit_gates
else :
if isinstance(gate, bqgate.TwoQubitGate) :
# controlled gate
for ctrlidx, qregidx in gate.control_target_iter(self.n_qubits) :
gt = typeinfo[0](*gate.params)
g = model.Gate(gt)
ctrl = self.qregs[ctrlidx]
qreg = self.qregs[qregidx]
g.set_ctrllist(ctrl)
g.set_qreg(qreg)
glist.append(g)
elif isinstance(gate, bqgate.ToffoliGate) :
gt = typeinfo[0](*gate.params)
g = model.Gate(gt)
c0, c1, t = gate.targets
ctrls = (self.qregs[c0], self.qregs[c1])
qreg = self.qregs[t]
g.set_ctrllist(ctrls)
class CU2Gate(TwoQubitGate):
"""Controlled U2 gate"""
def __init__(self, targets, phi, lambd, **kwargs):
super().__init__(targets, (phi, lambd), **kwargs)
self.phi = phi
self.lambd = lambd
lowername = "cu2"
def fallback(self, n_qubits):
return self._make_fallback_for_control_target_iter(
n_qubits, lambda c, t: [CU3Gate((c, t), math.pi / 2, self.phi, self.lambd)])
class CU3Gate(TwoQubitGate):
"""Controlled U3 gate"""
def __init__(self, targets, theta, phi, lambd, **kwargs):
super().__init__(targets, (theta, phi, lambd), **kwargs)
self.theta = theta
self.phi = phi
self.lambd = lambd
lowername = "cu3"
def fallback(self, n_qubits):
return self._make_fallback_for_control_target_iter(
n_qubits, lambda c, t: [
U1Gate(t, (self.lambd - self.phi) / 2),
CXGate((c, t)),
U3Gate(t, -self.theta / 2, 0, -(self.phi + self.lambd) / 2),
CXGate((c, t)),
super().__init__(targets, (theta,), **kwargs)
self.theta = theta
def _str_args(self):
return f'({self.theta})'
def fallback(self, n_qubits):
return self._make_fallback_for_control_target_iter(
n_qubits,
lambda c, t: [RYGate(t, self.theta / 2),
CXGate((c, t)),
RYGate(t, -self.theta / 2),
CXGate((c, t))])
class CRZGate(TwoQubitGate):
"""Rotate-Z gate"""
lowername = "crz"
def __init__(self, targets, theta, **kwargs):
super().__init__(targets, (theta,), **kwargs)
self.theta = theta
def _str_args(self):
return f'({self.theta})'
def fallback(self, n_qubits):
return self._make_fallback_for_control_target_iter(
n_qubits,
lambda c, t: [RZGate(t, self.theta / 2),
CXGate((c, t)),
RZGate(t, -self.theta / 2),
super().__init__(targets, (theta,), **kwargs)
self.theta = theta
def _str_args(self):
return f'({self.theta})'
def fallback(self, n_qubits):
return self._make_fallback_for_control_target_iter(
n_qubits,
lambda c, t: [RXGate(t, self.theta / 2),
CZGate((c, t)),
RXGate(t, -self.theta / 2),
CZGate((c, t))])
class CRYGate(TwoQubitGate):
"""Rotate-Y gate"""
lowername = "cry"
def __init__(self, targets, theta, **kwargs):
super().__init__(targets, (theta,), **kwargs)
self.theta = theta
def _str_args(self):
return f'({self.theta})'
def fallback(self, n_qubits):
return self._make_fallback_for_control_target_iter(
n_qubits,
lambda c, t: [RYGate(t, self.theta / 2),
CXGate((c, t)),
RYGate(t, -self.theta / 2),
self.lambd = lambd
lowername = "cu1"
def fallback(self, n_qubits):
return self._make_fallback_for_control_target_iter(
n_qubits, lambda c, t: [
U1Gate(c, self.lambd / 2),
CXGate((c, t)),
U1Gate(t, -self.lambd / 2),
CXGate((c, t)),
U1Gate(t, self.lambd / 2),
])
class CU2Gate(TwoQubitGate):
"""Controlled U2 gate"""
def __init__(self, targets, phi, lambd, **kwargs):
super().__init__(targets, (phi, lambd), **kwargs)
self.phi = phi
self.lambd = lambd
lowername = "cu2"
def fallback(self, n_qubits):
return self._make_fallback_for_control_target_iter(
n_qubits, lambda c, t: [CU3Gate((c, t), math.pi / 2, self.phi, self.lambd)])
class CU3Gate(TwoQubitGate):
"""Controlled U3 gate"""
def __init__(self, targets, theta, phi, lambd, **kwargs):
super().__init__(targets, (theta, phi, lambd), **kwargs)
return self._make_fallback_for_target_iter(
n_qubits, lambda t: [U3Gate(t, math.pi / 2, self.phi, self.lambd)])
class U3Gate(OneQubitGate):
"""U3 gate"""
def __init__(self, targets, theta, phi, lambd, **kwargs):
super().__init__(targets, (theta, phi, lambd), **kwargs)
self.theta = theta
self.phi = phi
self.lambd = lambd
lowername = "u3"
class CU1Gate(TwoQubitGate):
"""Controlled U1 gate"""
def __init__(self, targets, lambd, **kwargs):
super().__init__(targets, (lambd,), **kwargs)
self.lambd = lambd
lowername = "cu1"
def fallback(self, n_qubits):
return self._make_fallback_for_control_target_iter(
n_qubits, lambda c, t: [
U1Gate(c, self.lambd / 2),
CXGate((c, t)),
U1Gate(t, -self.lambd / 2),
CXGate((c, t)),
U1Gate(t, self.lambd / 2),
])
class ZGate(OneQubitGate):
"""Pauli's Z Gate"""
lowername = "z"
class HGate(OneQubitGate):
"""Hadamard Gate"""
lowername = "h"
class CZGate(TwoQubitGate):
"""Control-Z gate"""
lowername = "cz"
class CXGate(TwoQubitGate):
"""Control-X (CNOT) Gate"""
lowername = "cx"
class RXGate(OneQubitGate):
"""Rotate-X gate"""
lowername = "rx"
def __init__(self, targets, theta, **kwargs):
super().__init__(targets, (theta,), **kwargs)
self.theta = theta
def _str_args(self):
return f'({self.theta})'
super().__init__(targets, (theta,), **kwargs)
self.theta = theta
def _str_args(self):
return f'({self.theta})'
def fallback(self, n_qubits):
return self._make_fallback_for_control_target_iter(
n_qubits,
lambda c, t: [RZGate(t, self.theta / 2),
CXGate((c, t)),
RZGate(t, -self.theta / 2),
CXGate((c, t))])
class SwapGate(TwoQubitGate):
"""Swap gate"""
lowername = "swap"
def fallback(self, n_qubits):
return self._make_fallback_for_control_target_iter(
n_qubits,
lambda c, t: [CXGate((c, t)), CXGate((t, c)), CXGate((c, t))])
class ToffoliGate(Gate):
"""Toffoli (CCX) gate"""
lowername = "ccx"
def fallback(self, n_qubits):
c1, c2, t = self.targets
return [
class YGate(OneQubitGate):
"""Pauli's Y Gate"""
lowername = "y"
class ZGate(OneQubitGate):
"""Pauli's Z Gate"""
lowername = "z"
class HGate(OneQubitGate):
"""Hadamard Gate"""
lowername = "h"
class CZGate(TwoQubitGate):
"""Control-Z gate"""
lowername = "cz"
class CXGate(TwoQubitGate):
"""Control-X (CNOT) Gate"""
lowername = "cx"
class RXGate(OneQubitGate):
"""Rotate-X gate"""
lowername = "rx"
def __init__(self, targets, theta, **kwargs):
super().__init__(targets, (theta,), **kwargs)
self.theta = theta
"""S gate"""
lowername = "s"
def fallback(self, n_qubits):
return self._make_fallback_for_target_iter(n_qubits, lambda t: [RZGate(t, math.pi / 2)])
class SDagGate(OneQubitGate):
"""Dagger of S gate"""
lowername = "sdg"
def fallback(self, n_qubits):
return self._make_fallback_for_target_iter(n_qubits, lambda t: [RZGate(t, -math.pi / 2)])
class CRXGate(TwoQubitGate):
"""Rotate-X gate"""
lowername = "crx"
def __init__(self, targets, theta, **kwargs):
super().__init__(targets, (theta,), **kwargs)
self.theta = theta
def _str_args(self):
return f'({self.theta})'
def fallback(self, n_qubits):
return self._make_fallback_for_control_target_iter(
n_qubits,
lambda c, t: [RXGate(t, self.theta / 2),
CZGate((c, t)),
RXGate(t, -self.theta / 2),