Exemplo n.º 1
0
    def test_matvec_0(self):
        """Testing with zero term."""
        qubit_operator = QubitOperator.zero()

        vec = numpy.array([1, 2, 3, 4, 5, 6, 7, 8])
        matvec_expected = numpy.zeros(vec.shape)

        self.assertTrue(numpy.allclose(
            LinearQubitOperator(qubit_operator, 3) * vec, matvec_expected))
def benchmark_linear_qubit_operator(n_qubits, n_terms, processes=None):
    """Test speed with getting a linear operator from a Qubit Operator.

    Args:
        n_qubits: The number of qubits, implying the dimension of the operator
            is 2 ** n_qubits.
        n_terms: The number of terms in a qubit operator.
        processes: The number of processors to use.

    Returns:
        runtime_operator: The time it takes to get the linear operator.
        runtime_matvec: The time it takes to perform matrix multiplication.
    """
    # Generates Qubit Operator with specified number of terms.
    map_int_to_operator = {
        0: 'X',
        1: 'Y',
        2: 'Z',
    }
    qubit_operator = QubitOperator.zero()
    for _ in range(n_terms):
        tuples = []
        for i in range(n_qubits):
            operator = numpy.random.randint(4)
            # 3 is 'I', so just skip.
            if operator > 2:
                continue
            tuples.append((i, map_int_to_operator[operator]))
        if tuples:
            qubit_operator += QubitOperator(tuples, 1.00)

    # Gets an instance of (Parallel)LinearQubitOperator.
    start = time.time()
    if processes is None:
        linear_operator = LinearQubitOperator(qubit_operator, n_qubits)
    else:
        linear_operator = ParallelLinearQubitOperator(
            qubit_operator, n_qubits,
            LinearQubitOperatorOptions(processes=processes))

    end = time.time()
    runtime_operator = end - start

    vec = numpy.random.rand(2**n_qubits)
    # Performs matrix multiplication.
    start = time.time()
    _ = linear_operator * vec
    end = time.time()
    runtime_matvec = end - start
    return runtime_operator, runtime_matvec
Exemplo n.º 3
0
def benchmark_get_linear_qubit_operator(n_qubits, n_terms):
    """Test speed with getting a linear operator from a Qubit Operator.

    Args:
        n_qubits: The number of qubits, implying the dimension of the operator
            is 2 ** n_qubits.
        n_terms: The number of terms in a qubit operator.

    Returns:
        runtime_operator: The time it takes to get the linear operator.
        runtime_matvec: The time it takes to perform matrix multiplication.
    """
    # Generates Qubit Operator with specified number of terms.
    m = {
        0: 'X',
        1: 'Y',
        2: 'Z',
    }
    qubit_operator = QubitOperator.zero()
    for _ in xrange(n_terms):
        tuples = []
        for i in xrange(n_qubits):
            op = numpy.random.randint(4)
            # 3 is 'I', so just skip.
            if op > 2:
                continue
            tuples.append((i, m[op]))
        if tuples:
            qubit_operator += QubitOperator(tuples, 1.00)

    # Gets an instance of LinearOperator.
    start = time.time()
    linear_operator = get_linear_qubit_operator(qubit_operator)
    end = time.time()
    runtime_operator = end - start

    vec = numpy.random.rand(2**n_qubits)
    # Performs matrix multiplication.
    start = time.time()
    matvec = linear_operator * vec
    end = time.time()
    runtime_matvec = end - start
    return runtime_operator, runtime_matvec
Exemplo n.º 4
0
    def test_get_lowest_n(self):
        """Test for get_lowest_n()."""
        dimension = 2**self.n_qubits
        qubit_operator = QubitOperator.zero()
        for i in range(min(self.n_qubits, 4)):
            numpy.random.seed(dimension + i)
            qubit_operator += QubitOperator(((i, 'Z'), ),
                                            numpy.random.rand(1)[0])
        qubit_operator *= self.coefficient
        davidson = QubitDavidson(qubit_operator, self.n_qubits)

        n_lowest = 6
        numpy.random.seed(dimension)
        initial_guess = numpy.random.rand(dimension, n_lowest)
        success, eigen_values, eigen_vectors = davidson.get_lowest_n(
            n_lowest, initial_guess, max_iterations=20)

        expected_eigen_values = -3.80376934 * numpy.ones(n_lowest)

        self.assertTrue(success)
        self.assertTrue(numpy.allclose(eigen_values, expected_eigen_values))
        self.assertAlmostEqual(
            get_difference(davidson.linear_operator, eigen_values,
                           eigen_vectors), 0)