Exemple #1
0
 def array(self):
     half_theta = np.pi * self.phase
     return np.array([
         1, 0, 0, 0, 0, 1, 0, 0, 0, 0,
         np.exp(-1j * half_theta), 0, 0, 0, 0,
         np.exp(1j * half_theta)
     ])
Exemple #2
0
 def measure(dim, destructive=True):
     """ Measure a quantum dimension into a classical dimension. """
     if not dim:
         return CQMap(CQ(), CQ(), np.array(1))
     if len(dim) == 1:
         if destructive:
             array = np.array([
                 int(i == j == k) for i in range(dim[0])
                 for j in range(dim[0]) for k in range(dim[0])
             ])
             return CQMap(Q(dim), C(dim), array)
         array = np.array([
             int(i == j == k == l == m) for i in range(dim[0])
             for j in range(dim[0]) for k in range(dim[0])
             for l in range(dim[0]) for m in range(dim[0])
         ])
         return CQMap(Q(dim), C(dim) @ Q(dim), array)
     return CQMap.measure(dim[:1], destructive=destructive)\
         @ CQMap.measure(dim[1:], destructive=destructive)
Exemple #3
0
 def __init__(self, name, n_qubits, array=None, data=None, _dagger=False):
     dom = qubit**n_qubits
     if array is not None:
         self._array = np.array(array).reshape(2 * n_qubits * (2, ) or 1)
     super().__init__(name,
                      dom,
                      dom,
                      is_mixed=False,
                      data=data,
                      _dagger=_dagger)
Exemple #4
0
 def __init__(self, name, n_bits_in, n_bits_out, array=None, _dagger=False):
     dom, cod = bit**n_bits_in, bit**n_bits_out
     if array is not None:
         array = np.array(array).reshape((n_bits_in + n_bits_out) * (2, )
                                         or 1)
     super().__init__(name,
                      dom,
                      cod,
                      is_mixed=False,
                      data=array,
                      _dagger=_dagger)
Exemple #5
0
 def __init__(self, name, n_qubits, array=None, data=None, _dagger=False):
     """
     >>> g = CX
     >>> assert g.dom == g.cod == PRO(2)
     """
     if array is not None:
         self._array = np.array(array).reshape(2 * n_qubits * (2, ) or 1)
     Box.__init__(self,
                  name,
                  PRO(n_qubits),
                  PRO(n_qubits),
                  data=data,
                  _dagger=_dagger)
     Circuit.__init__(self, n_qubits, n_qubits, [self], [0])
Exemple #6
0
 def __init__(self, name, n_bits_in, n_bits_out, data=None, _dagger=False):
     dom, cod = bit**n_bits_in, bit**n_bits_out
     if isinstance(data, Callable):
         self.is_linear = False
     else:
         self.is_linear = True
         data = np.array(data).reshape((n_bits_in + n_bits_out) * (2, )
                                       or (1, ))
     super().__init__(name,
                      dom,
                      cod,
                      is_mixed=False,
                      data=data,
                      _dagger=_dagger)
Exemple #7
0
 def array(self):
     """
     >>> assert CRz(0).array[-1] == 1
     """
     phase = np.exp(1j * 2 * np.pi * self.phase)
     return np.array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, phase])
Exemple #8
0
 def array(self):
     half_theta = np.pi * self.phase
     global_phase = np.exp(1j * half_theta)
     sin, cos = np.sin(half_theta), np.cos(half_theta)
     return global_phase * np.array([[cos, -1j * sin], [-1j * sin, cos]])
Exemple #9
0
 def array(self):
     """
     >>> assert np.allclose(Rz(0.5).array, Z.array)
     """
     theta = 2 * np.pi * self.phase
     return np.array([[1, 0], [0, np.exp(1j * theta)]])
Exemple #10
0
    return Gate('scalar({:.3f})'.format(complex),
                0,
                complex,
                _dagger=None if np.conjugate(complex) == complex else False)


SWAP = Gate('SWAP',
            2, [1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1],
            _dagger=None)
CX = Gate('CX',
          2, [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0],
          _dagger=None)
CZ = Gate('CZ',
          2, [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, -1],
          _dagger=None)
H = Gate('H', 1, 1 / np.sqrt(2) * np.array([1, 1, 1, -1]), _dagger=None)
S = Gate('S', 1, [1, 0, 0, 1j])
T = Gate('T', 1, [1, 0, 0, np.exp(1j * np.pi / 4)])
X = Gate('X', 1, [0, 1, 1, 0], _dagger=None)
Y = Gate('Y', 1, [0, -1j, 1j, 0])
Z = Gate('Z', 1, [1, 0, 0, -1], _dagger=None)


def Euler(thetas):
    return Rx(thetas[0]) >> Rz(thetas[1]) >> Rx(thetas[2])


def Hlayer(n):
    layer = H
    for nn in range(1, n):
        layer = layer @ H
Exemple #11
0
 def array(self):
     half_theta = np.pi * self.phase
     cos, sin = np.cos(half_theta), np.sin(half_theta)
     return np.array([
         1, 0, 0, 0, 0, 1, 0, 0, 0, 0, cos, -1j * sin, 0, 0, -1j * sin, cos
     ])
Exemple #12
0
 def array(self):
     theta = 2 * np.pi * self.phase
     return np.array(
         [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0,
          np.exp(1j * theta)])
Exemple #13
0
 def array(self):
     half_theta = np.pi * self.phase
     return np.array([[np.exp(-1j * half_theta), 0],
                      [0, np.exp(1j * half_theta)]])
Exemple #14
0
 def array(self):
     half_theta = np.pi * self.phase
     sin, cos = np.sin(half_theta), np.cos(half_theta)
     return np.array([[cos, -1j * sin], [-1j * sin, cos]])
Exemple #15
0
 def array(self):
     phase = np.exp(1j * 2 * np.pi * self.phase)
     return np.array([1, 0, 0, 0,
                      0, 1, 0, 0,
                      0, 0, 1, 0,
                      0, 0, 0, phase])