コード例 #1
0
def modifiedGramSchmidt(A):
    # assuming A is a square matrix
    dim = A.shape[0]
    Q = np.zeros(A.shape, dtype=A.dtype)
    for j in range(0, dim):
        q = A[:, j]
        for i in range(0, j):
            rij = np.vdot(Q[:, i], q)
            q = q - rij * Q[:, i]
        rjj = np.linalg.norm(q, ord=2)
        if np.isclose(rjj, 0.0):
            raise ValueError("invalid input matrix")
        else:
            Q[:, j] = q / rjj
    return Q
コード例 #2
0
ファイル: test_qnode.py プロジェクト: wuben3125/pennylane
    def test_multiple_expectation_different_wires(self):
        "Tests that qnodes return multiple expectation values."
        self.logTestName()

        a, b, c = 0.5, 0.54, 0.3

        @qml.qnode(self.dev2)
        def circuit(x, y, z):
            qml.RX(x, [0])
            qml.RZ(y, [0])
            qml.CNOT([0, 1])
            qml.RY(y, [0])
            qml.RX(z, [0])
            return qml.expval.PauliY(0), qml.expval.PauliZ(1)

        res = circuit(a, b, c)

        out_state = np.kron(Rotx(c), I) @ np.kron(Roty(b), I) @ CNOT \
            @ np.kron(Rotz(b), I) @ np.kron(Rotx(a), I) @ np.array([1, 0, 0, 0])

        ex0 = np.vdot(out_state, np.kron(Y, I) @ out_state)
        ex1 = np.vdot(out_state, np.kron(I, Z) @ out_state)
        ex = np.array([ex0, ex1])
        self.assertAllAlmostEqual(ex, res, delta=self.tol)
コード例 #3
0
def real_ip(u, v):
    """Real inner product of complex vectors."""
    ip = np.real(np.vdot(u, v))
    return ip