Ejemplo n.º 1
0
def purification(de_A):

    # representation of the input state with orthogonal basis of system A
    coef, basis_A = eigen_values_vectors(de_A)
    rank = len(basis_A)

    # make computational basis of system R
    qnum_R = int(math.log2(rank))
    if 2**qnum_R != rank:
        qnum_R += 1
    basis_R = computational_basis(qnum_R)

    # orthogonal basis of system A+R
    basis_AR = [np.kron(basis_A[i], basis_R[i]) for i in range(rank)]

    # representation of the purified state with orthogonal basis of system A+R
    vec_AR = np.array([0] * len(basis_AR[0]))
    for i in range(rank):
        vec_AR = vec_AR + (math.sqrt(coef[i]) * basis_AR[i])

    qs_AR = QState(vector=vec_AR)
    de_AR = DensOp(qstate=[qs_AR], prob=[1.0])

    # free memory
    qs_AR.free()

    return de_AR
Ejemplo n.º 2
0
def main():

    QState.swap = swap
    QState.qft2 = qft2
    QState.qft3 = qft3
    QState.qft = qft
    QState.iqft2 = iqft2
    QState.iqft3 = iqft3
    QState.iqft = iqft

    print("== initial state ==")
    qs = QState(3).h(1).h(0)
    qs.show()
    data_in = qs.amp

    print("== QFT ==")
    qs.qft([0, 1, 2])
    qs.show()

    print("== FFT (numpy) ==")
    data_fft = np.fft.ifft(data_in)
    norm = np.linalg.norm(data_fft, ord=2)
    data_fft /= norm
    pprint(data_fft)

    qs.free()
Ejemplo n.º 3
0
 def test_m(self):
     """test 'm' (some angle and phase)
     """
     qs = QState(qubit_num=2).ry(1, phase=0.2).rz(1, phase=0.2)
     md = qs.m([1], shots=10, angle=0.2, phase=0.2)
     qs.free()
     self.assertEqual(md.frq[0], 10)
     self.assertEqual(md.frq[1], 0)
Ejemplo n.º 4
0
 def test_bloch(self):
     """test 'bloch'
     """
     qs = QState(qubit_num=3).ry(0, phase=0.25).rz(0, phase=0.25)
     theta, phi = qs.bloch()
     qs.free()
     ans = (round(theta, 4) == 0.25 and round(phi, 4) == 0.25)
     self.assertEqual(ans, True)
Ejemplo n.º 5
0
 def test_h_u2(self):
     """test 'u2' gate (following 'h' gate)
     """
     qs = QState(qubit_num=1).h(0).u2(0, alpha=0.1, beta=0.2)
     actual = qs.amp
     expect = np.array([0.02447174 - 0.1545085j, 0.69840112 + 0.69840112j])
     ans = equal_vectors(actual, expect)
     qs.free()
     self.assertEqual(ans, True)
Ejemplo n.º 6
0
 def test_u2(self):
     """test 'u2' gate
     """
     qs = QState(qubit_num=1).u2(0, alpha=0.1, beta=0.2)
     actual = qs.amp
     expect = np.array([0.70710678 + 0.j, 0.5720614 + 0.41562694j])
     ans = equal_vectors(actual, expect)
     qs.free()
     self.assertEqual(ans, True)
Ejemplo n.º 7
0
 def test_u1(self):
     """test 'u1' gate
     """
     qs = QState(qubit_num=1).u1(0, alpha=0.1)
     actual = qs.amp
     expect = np.array([1.0, 0.0])
     ans = equal_vectors(actual, expect)
     qs.free()
     self.assertEqual(ans, True)
Ejemplo n.º 8
0
 def test_h_y(self):
     """test 'y' gate (following 'h' gate)
     """
     qs = QState(qubit_num=1).h(0).y(0)
     actual = qs.amp
     expect = np.array([-1.0j / SQRT_2, 1.0j / SQRT_2])
     ans = equal_vectors(actual, expect)
     qs.free()
     self.assertEqual(ans, True)
Ejemplo n.º 9
0
 def test_sw(self):
     """test 'sw' gate (following 'x' gate, not 'h' gates)
     """
     qs = QState(qubit_num=2).x(0).sw(0, 1)
     actual = qs.amp
     expect = np.array([0j, (1 + 0j), 0j, 0j])
     ans = equal_vectors(actual, expect)
     qs.free()
     self.assertEqual(ans, True)
Ejemplo n.º 10
0
 def test_init(self):
     """test '__new__' (qubit_num)
     """
     qs = QState(qubit_num=3)
     actual = qs.amp
     expect = np.array([1j, 0j, 0j, 0j, 0j, 0j, 0j, 0j])
     ans = equal_vectors(actual, expect)
     qs.free()
     self.assertEqual(ans, True)
Ejemplo n.º 11
0
 def test_h_ry(self):
     """test 'ry' gate (following 'h' gate)
     """
     qs = QState(qubit_num=1).h(0).ry(0, phase=0.25)
     actual = qs.amp
     expect = np.array([0.38268343 + 0.j, 0.92387953 + 0.j])
     ans = equal_vectors(actual, expect)
     qs.free()
     self.assertEqual(ans, True)
Ejemplo n.º 12
0
 def test_ry(self):
     """test 'ry' gate
     """
     qs = QState(qubit_num=1).ry(0, phase=0.25)
     actual = qs.amp
     expect = np.array([COS_PI_8, SIN_PI_8])
     ans = equal_vectors(actual, expect)
     qs.free()
     self.assertEqual(ans, True)
Ejemplo n.º 13
0
 def test_h_t_dg(self):
     """test 't_dg' gate (following 'h' gate)
     """
     qs = QState(qubit_num=1).h(0).t_dg(0)
     actual = qs.amp
     expect = np.array([1.0 / SQRT_2, 0.5 - 0.5j])
     ans = equal_vectors(actual, expect)
     qs.free()
     self.assertEqual(ans, True)
Ejemplo n.º 14
0
 def test_t_dg(self):
     """test 't_dg' gate
     """
     qs = QState(qubit_num=1).t_dg(0)
     actual = qs.amp
     expect = np.array([1.0, 0.0])
     ans = equal_vectors(actual, expect)
     qs.free()
     self.assertEqual(ans, True)
Ejemplo n.º 15
0
 def test_h_h(self):
     """test 'h' gate (following 'h')
     """
     qs = QState(qubit_num=1).h(0).h(0)
     actual = qs.amp
     expect = np.array([1.0, 0.0])
     ans = equal_vectors(actual, expect)
     qs.free()
     self.assertEqual(ans, True)
Ejemplo n.º 16
0
 def test_h(self):
     """test 'h' gate
     """
     qs = QState(qubit_num=1).h(0)
     actual = qs.amp
     expect = np.array([1.0 / SQRT_2, 1.0 / SQRT_2])
     ans = equal_vectors(actual, expect)
     qs.free()
     self.assertEqual(ans, True)
Ejemplo n.º 17
0
 def test_xr_dg(self):
     """test 'xr_dg' gate
     """
     qs = QState(qubit_num=1).xr_dg(0)
     actual = qs.amp
     expect = np.array([0.5 - 0.5j, 0.5 + 0.5j])
     ans = equal_vectors(actual, expect)
     qs.free()
     self.assertEqual(ans, True)
Ejemplo n.º 18
0
 def test_u3(self):
     """test 'u3' gate
     """
     qs = QState(qubit_num=1).u3(0, alpha=0.1, beta=0.2, gamma=0.3)
     actual = qs.amp
     expect = np.array([0.89100652 + 0.j, 0.36728603 + 0.26684892j])
     ans = equal_vectors(actual, expect)
     qs.free()
     self.assertEqual(ans, True)
Ejemplo n.º 19
0
 def test_h_u3(self):
     """test 'u3' gate (following 'h' gate)
     """
     qs = QState(qubit_num=1).h(0).u3(0, alpha=0.1, beta=0.2, gamma=0.3)
     actual = qs.amp
     expect = np.array([0.32472882 - 0.09920056j, 0.63003676 + 0.69840112j])
     ans = equal_vectors(actual, expect)
     qs.free()
     self.assertEqual(ans, True)
Ejemplo n.º 20
0
 def test_h_rz(self):
     """test 'rz' gate (following 'h' gate)
     """
     qs = QState(qubit_num=1).h(0).rz(0, phase=0.25)
     actual = qs.amp
     expect = np.array([0.65328148 - 0.27059805j, 0.65328148 + 0.27059805j])
     ans = equal_vectors(actual, expect)
     qs.free()
     self.assertEqual(ans, True)
Ejemplo n.º 21
0
 def test_sw(self):
     """test 'sw' gate
     """
     qs = QState(qubit_num=2).h(0).h(1).sw(0, 1)
     actual = qs.amp
     expect = np.array([(0.5 + 0j), (0.5 + 0j), (0.5 + 0j), (0.5 + 0j)])
     ans = equal_vectors(actual, expect)
     qs.free()
     self.assertEqual(ans, True)
Ejemplo n.º 22
0
 def test_mcx_3(self):
     """test 'mcx' gate (for 3-qubit)
     """
     qs = QState(qubit_num=3).x(0).x(1).mcx([0, 1, 2])
     actual = qs.amp
     expect = np.array([0j, 0j, 0j, 0j, 0j, 0j, 0j, (1 + 0j)])
     ans = equal_vectors(actual, expect)
     qs.free()
     self.assertEqual(ans, True)
Ejemplo n.º 23
0
 def test_p(self):
     """test 'p' gate
     """
     qs = QState(qubit_num=1).p(0, phase=0.25)
     actual = qs.amp
     expect = np.array([1.0, 0.0])
     ans = equal_vectors(actual, expect)
     qs.free()
     self.assertEqual(ans, True)
Ejemplo n.º 24
0
 def test_y(self):
     """test 'y' gate
     """
     qs = QState(qubit_num=1).y(0)
     actual = qs.amp
     expect = np.array([0.0, 1.0j])
     ans = equal_vectors(actual, expect)
     qs.free()
     self.assertEqual(ans, True)
Ejemplo n.º 25
0
 def test_x_x_csw(self):
     """test 'csw' gate
     """
     qs = QState(qubit_num=3).x(0).x(1).csw(0, 1, 2)
     actual = qs.amp
     expect = np.array([0j, 0j, 0j, 0j, 0j, (1 + 0j), 0j, 0j])
     ans = equal_vectors(actual, expect)
     qs.free()
     self.assertEqual(ans, True)
Ejemplo n.º 26
0
 def test_h_p(self):
     """test 'p' gate (following 'h' gate)
     """
     qs = QState(qubit_num=1).h(0).p(0, phase=0.25)
     actual = qs.amp
     expect = np.array([0.70710678, 0.5 + 0.5j])
     ans = equal_vectors(actual, expect)
     qs.free()
     self.assertEqual(ans, True)
Ejemplo n.º 27
0
 def test_h_u1(self):
     """test 'u1' gate (following 'h' gate)
     """
     qs = QState(qubit_num=1).h(0).u1(0, alpha=0.1)
     actual = qs.amp
     expect = np.array([0.70710678 + 0.j, 0.67249851 + 0.21850801j])
     ans = equal_vectors(actual, expect)
     qs.free()
     self.assertEqual(ans, True)
Ejemplo n.º 28
0
 def test_mz(self):
     """test 'mz' (for bell state)
     """
     qs = QState(qubit_num=2).h(0).cx(0, 1)
     md = qs.mz(shots=10)
     qs.free()
     self.assertEqual(md.frq[0] + md.frq[3], 10)
     self.assertEqual(md.frq[1], 0)
     self.assertEqual(md.frq[2], 0)
Ejemplo n.º 29
0
 def test_init_with_vector(self):
     """test '__new__' (vector)
     """
     vec = np.array([2j, 0j, 0j, 0j, 0j, 0j, 0j, 0j])
     qs = QState(vector=vec)
     actual = qs.amp
     expect = np.array([1j, 0j, 0j, 0j, 0j, 0j, 0j, 0j])
     ans = equal_vectors(actual, expect)
     qs.free()
     self.assertEqual(ans, True)
Ejemplo n.º 30
0
 def test_cu1(self):
     """test 'cu1' gate
     """
     qs = QState(qubit_num=2).h(0).h(1).cu1(0, 1, alpha=0.1)
     actual = qs.amp
     expect = np.array([(0.5 + 0j), (0.5 + 0j), (0.5 + 0j),
                        (0.47552825814757677 + 0.1545084971874737j)])
     ans = equal_vectors(actual, expect)
     qs.free()
     self.assertEqual(ans, True)