def test_large_matmul(verbose=0):
    """Testlarge  matrix multiplication. 
    
    For the mmgroup project wee have to multiply marices
    of type QStateMatrix with shape (12,12). So we'd
    better test this operation here!
    
    We generate random matrices m1, m2. We test the 
    multiplication of a random  r times 4096 submatrix
    of m1 with a random 4096 times r submatrix of m2.
    Here r is about 14.
    """
    for ntest, (m1, m2, r1, r2) in enumerate(large_matmul_testvectors()):
        if verbose:
            print("TEST %s QStateMatrix multiplication" % (ntest + 1))
            print(m1.echelon())
            print(m2.echelon())
        m3 = m1 @ m2
        if verbose:
            print("Product")
            print(m3)
        c1, c2 = m1[r1, :], m2[:, r2]
        c3 = m3[r1, r2]
        c3_ref = c1 @ c2
        err = "Matrix multiplication has failed"
        compare_complex(c3_ref, c3, err)
        m3 = m1 @ m1
        c1, c2 = m1[r1, :], m1[:, r2]
        c3 = m3[r1, r2]
        c3_ref = c1 @ c2
        err = "Matrix squaring has failed"
        compare_complex(c3_ref, c3, err)
def test_conj(verbose=0):
    """Test conjugation and transposition for `QStateMatrix``"""
    for ntest, m1 in enumerate(create_conj_testvectors()):
        if verbose:
            print("TEST %s Matrix multiplication of entries" % (ntest + 1))
            print(m1.echelon())
        if verbose:
            print("Conjugate")
            print(m1.conj())
        err = "Matrix conjugation failed"
        compare_complex(m1.conj()[:, :], m1[:, :].conj(), err)
        if verbose:
            print("Transpose")
            print(m1.T)
        err = "Matrix transpostion failed"
        compare_complex(m1.T[:, :], m1[:, :].T, err)
def test_matmul(verbose=0):
    """Test matrix multiplication. """
    for ntest, (m1, m2) in enumerate(create_matmul_testvectors()):
        if verbose:
            print("TEST %s QStateMatrix multiplication" % (ntest + 1))
            print(m1)
            print(m2)
        m3 = m1 @ m2
        c1, c2 = m1.complex(), m2.complex()
        c3 = m3.complex()
        c3_ref = c1 @ c2
        err = "Matrix multiplcation has failed"
        compare_complex(c3_ref, c3, err)
        if verbose:
            print("Product")
            print(m3)
Exemple #4
0
def test_gate_h(verbose=0):
    """Test function ``qstate12_gate_ctrl_phi``. """
    for ntest, (m, v) in enumerate(gate_h_testdata()):
        if verbose:
            print("TEST gate ctrl phi %s" % (ntest + 1))
            print(m)
            print("v=", hex(v))
        m1 = m.copy().gate_h(v)
        c, c1 = m.complex().ravel(), m1.complex().ravel()
        c1_ref = complex_gate_h(c, v)
        if verbose:
            print("output")
            print(m1)
        err = "Method gate_ctrl_phi has failed"
        compare_complex(c1_ref, c1, err)
        gm = qs_hadamard_matrix(m.shape[1], v)
        assert m @ gm == m1, [str(x) for x in (m, m @ gm, m1)]
Exemple #5
0
def test_gate_not(verbose=0):
    """Test function ``qstate12_gate_not``. """
    for ntest, (m, v) in enumerate(gate_not_testdata()):
        if verbose:
            print("TEST gate not %s" % (ntest + 1))
            print(m)
            print("m.shape=", m.shape, "v=", hex(v))
        m1 = m.copy().gate_not(v)
        c, c1 = m.complex().ravel(), m1.complex().ravel()
        c1_ref = complex_gate_not(c, v)
        if verbose:
            print("output")
            print(m1)
        err = "Method gate_not has failed"
        compare_complex(c1_ref, c1, err)
        gm = qs_pauli_matrix(m.shape[1], v << m.shape[1])
        assert m @ gm == m1, [str(x) for x in (m, gm, m @ gm, m1)]
Exemple #6
0
def test_rot(verbose=0):
    """Test function ``qstate12_rot_bits``. """
    for ntest, (m, rot, nrot, n0) in enumerate(rot_testdata()):
        if verbose:
            print("TEST rot bits %s" % (ntest + 1))
            print(m)
            print("rot=", rot, ", nrot =", nrot, ", n0 =", n0)
        m1 = m.copy().rot_bits(rot, nrot, n0)
        c, c1 = m.complex().ravel(), m1.complex().ravel()
        c1_ref = np.copy(c)
        for i, x in enumerate(c):
            c1_ref[rot_index(i, m.ncols, rot, nrot, n0)] = x
        if verbose:
            print("output")
            print(m1)
        err = "Method rot has failed"
        compare_complex(c1_ref, c1, err)
Exemple #7
0
def test_xch_bits(verbose=0):
    """Test function ``qstate12_xch_bits``. """
    for ntest, (m, sh, mask) in enumerate(xch_bits_testdata()):
        if verbose:
            print("TEST xch bits %s" % (ntest + 1))
            print(m)
            print("sh=", sh, ", mask =", mask)
        m1 = m.copy().xch_bits(sh, mask)
        c, c1 = m.complex().ravel(), m1.complex().ravel()
        c1_ref = np.copy(c)
        for i, x in enumerate(c):
            c1_ref[xch_bits_index(i, m.ncols, sh, mask)] = x
        if verbose:
            print("output")
            print(m1)
        err = "Method xch_bits has failed"
        compare_complex(c1_ref, c1, err)
def test_qs_product(verbose=0):
    """Test function ``qstate12_product``. """
    for ntest, (m1, m2, nqb) in enumerate(create_product_testvectors()):
        nc = randint(0, nqb)
        if verbose:
            print("TEST %s" % (ntest + 1))
            print(m1)
            print(m2)
            print("nqb =", nqb, " nc =", nc)
            print("Output states")
        try:
            err = "Execution of function qstate12_product() has failed"
            m3 = flat_product(m1, m2, nqb, nc)
        except ValueError:
            print("\n" + err + "\nInput states:")
            print(m1)
            print(m2)
            print("nqb =", nqb, ", nc =", nc)
            row_pos, pm1, pm2 = qstate12_prep_mul(m1, m2, nqb)
            print("prep_mul, row pos = ", row_pos)
            print(QStateMatrix(pm1))
            print(QStateMatrix(pm2))
            print("\nOutput states:")
            qm1, qm2 = m1.copy(), m2.copy()
            qm1.qstate12_product(qm2, nqb, nc)
            print(m1)
            print(m2)
            raise
        c1, c2, c3 = m1.complex(), m2.complex(), m3.complex()
        c3_ref = qs_complex_prod(c1, c2, nqb, nc)
        try:
            err = "Function flat_product() has failed"
            compare_complex(c3_ref, c3, err)
        except ValueError:
            print("\n" + err + "\nInput states:")
            print(m1)
            print(m2)
            print("nqb =", nqb, ", nc =", nc)
            print("\nOutput states:")
            print(m3)
            raise
        if verbose:
            print("Output state")
            print(m3)
Exemple #9
0
def test_restrict(verbose=0):
    """Test function ``qstate12_restrict``. """
    for ntest, (m, j, nqb) in enumerate(restrict_testdata()):
        if verbose:
            print("TEST restrict %s" % (ntest + 1))
            print(m)
            print("j=", j, ", nqb =", nqb)
        m1 = m.copy().restrict(j, nqb)
        c, c1 = m.complex().ravel(), m1.complex().ravel()
        c1_ref = restrict_complex(c, j, nqb)
        if verbose:
            print("output after restrict")
            print(m1)
        err = "Method restrict has failed"
        compare_complex(c1_ref, c1, err)

        m1 = m.copy().restrict_zero(j, nqb)
        c, c1 = m.complex().ravel(), m1.complex().ravel()
        c1_ref = restrict_complex(c, j, nqb, zero=True)
        if verbose:
            print("output after restrict_zero")
            print(m1)
        err = "Method restrict_zero has failed"
        compare_complex(c1_ref, c1, err)

        m1 = m.copy().sumup(j, nqb)
        c, c1 = m.complex().ravel(), m1.complex().ravel()
        c1_ref = sumup_complex(c, j, nqb)
        if verbose:
            print("output after sumup")
            print(m1)
        err = "Method sumup has failed"
        compare_complex(c1_ref, c1, err)
Exemple #10
0
def test_gate_phi(verbose=0):
    """Test function ``qstate12_gate_phi``. """
    for ntest, (m, v, phi) in enumerate(gate_phi_testdata()):
        assert 0 <= v < 1 << m.shape[1]
        if verbose:
            print("TEST gate phi %s" % (ntest + 1))
            print(m)
            print("v=", hex(v))
            print("phi=", phi & 3)
        m1 = m.copy().gate_phi(v, phi)
        c, c1 = m.complex().ravel(), m1.complex().ravel()
        c1_ref = complex_gate_phi(c, v, phi)
        if verbose:
            print("output")
            print(m1)
        err = "Method gate_phi has failed"
        compare_complex(c1_ref, c1, err)
        gm = qs_phi_matrix(m.shape[1], v, phi)
        assert m @ gm == m1, [str(x) for x in (m, m @ gm, m1)]
        if (phi & 3 == 2):
            gm = qs_pauli_matrix(m.shape[1], v)
            assert m @ gm == m1, [str(x) for x in (m, m @ gm, m1)]
Exemple #11
0
def test_extend(verbose=0):
    """Test function ``qstate12_extend``. """
    for ntest, (m, j, nqb) in enumerate(extend_testdata()):
        if verbose:
            print("TEST extend %s" % (ntest + 1))
            print(m)
            print("j=", j, ", nqb =", nqb)
        m1 = m.copy().extend(j, nqb)
        c, c1 = m.complex().ravel(), m1.complex().ravel()
        c1_ref = extend_complex(c, j, nqb)
        if verbose:
            print("output after extend")
            print(m1)
        err = "Method extend has failed"
        compare_complex(c1_ref, c1, err)

        m1 = m.copy().extend_zero(j, nqb)
        c, c1 = m.complex().ravel(), m1.complex().ravel()
        c1_ref = extend_complex(c, j, nqb, zero=True)
        if verbose:
            print("output after extend_zero")
            print(m1)
        err = "Method extend_zero has failed"
        compare_complex(c1_ref, c1, err)
def test_mul(verbose=0):
    """Test elementwise and scalar multiplication for `QStateMatrix``"""
    for ntest, (m1, m2) in enumerate(create_mul_testvectors()):
        if verbose:
            print("TEST %s Matrix multiplication of entries" % (ntest + 1))
            print(m1.echelon())
            print(m2.echelon())
        m3 = (m1 * m2)
        if verbose:
            print("Product")
            print(m3)
        c3 = m3[:, :]
        c3_ref = m1[:, :] * m2[:, :]
        err = "Matrix multiplcation of entries has failed"
        compare_complex(c3_ref, c3, err)
        err = "Matrix negation has failed"
        compare_complex(-(m1[:, :]), (-m1)[:, :], err)
        err = "Matrix scalar multiplication failed"
        f = (1 + 1j)**randint(-8, 7) * 2**(0.5 * randint(-10, 10))
        compare_complex(f * (m1[:, :]), (f * m1)[:, :], err)
        compare_complex(f * (m1[:, :]), (m1 * f)[:, :], err)
        compare_complex((m1[:, :] / f), (m1 / f)[:, :], err)
        if ntest < 20:
            compare_complex(0 * (m1[:, :]), (0 * m1)[:, :], err)