Ejemplo n.º 1
0
    def __init__(self, q_system, excited_state=0):
        self.q_system = q_system

        H_sparse_matrix = get_sparse_operator(q_system.jw_qubit_ham)
        if excited_state > 0:
            # H_sparse_matrix = backend. ham_sparse_matrix(q_system, excited_state=excited_state)
            H_sparse_matrix = get_sparse_operator(q_system.jw_qubit_ham)
            if excited_state > 0:
                H_lower_state_terms = q_system.H_lower_state_terms
                assert H_lower_state_terms is not None
                assert len(H_lower_state_terms) >= excited_state
                for i in range(excited_state):
                    term = H_lower_state_terms[i]
                    state = term[1]
                    statevector = QiskitSimBackend.statevector_from_ansatz(state.ansatz_elements, state.parameters, state.n_qubits,
                                                                           state.n_electrons, init_state_qasm=state.init_state_qasm)
                    # add the outer product of the lower lying state to the Hamiltonian
                    H_sparse_matrix += scipy.sparse.csr_matrix(term[0] * numpy.outer(statevector, statevector))

        if H_sparse_matrix.data.nbytes > config.matrix_size_threshold:
            # decrease the size of the matrix. Typically it will have a lot of insignificant very small (~1e-19)
            # elements that do not contribute to the accuracy but inflate the size of the matrix (~200 MB for Lih)
            logging.warning('Hamiltonian sparse matrix accuracy decrease!!!')
            H_sparse_matrix = scipy.sparse.csr_matrix(H_sparse_matrix.todense().round(config.floating_point_accuracy_digits))

        super(GlobalCache, self).__init__(H_sparse_matrix=H_sparse_matrix, n_qubits=q_system.n_qubits,
                                          n_electrons=q_system.n_electrons, commutators_sparse_matrices_dict=None)
Ejemplo n.º 2
0
    excitation = SpinCompDFExc([0, 2], [3, 5], n_qubits)
    # excitation = SpinCompDFExc([0, 1], [2, 3], n_qubits)
    excitation_gen_matrices = [get_sparse_operator(excitation.excitations_generators[0], n_qubits),
                               get_sparse_operator(excitation.excitations_generators[1], n_qubits)]
    excitation_matrix_1 = scipy.sparse.linalg.expm(parameter * excitation_gen_matrices[1]) *\
                          scipy.sparse.linalg.expm(parameter * excitation_gen_matrices[0])

    excitation_matrix_2 = identity
    for exc_gen_mat in excitation_gen_matrices[::-1]:
        term1 = numpy.sin(parameter)*exc_gen_mat
        term2 = (1 - numpy.cos(parameter)) * exc_gen_mat*exc_gen_mat
        excitation_matrix_2 *= identity + term1 + term2

    # excitation_matrix_0 = get_circuit_matrix(QasmUtils.qasm_header(n_qubits) + SpinCompDFExc([5, 3], [2, 0], n_qubits).get_qasm([parameter]))

    statevector_0 = QiskitSimBackend.statevector_from_ansatz([excitation], [parameter], n_qubits, n_electrons).round(10)
    statevector_1 = excitation_matrix_1.dot(scipy.sparse.csr_matrix(hf_statevector).transpose().conj()).\
        transpose().conj().todense().round(10)

    statevector_2 = excitation_matrix_2.dot(scipy.sparse.csr_matrix(hf_statevector).transpose().conj()).\
        transpose().conj().todense().round(10)

    exc_gen_sparse_matrices_dict = {str(excitation.excitations_generators): excitation_gen_matrices}
    sqr_exc_gen_sparse_matrices_dict = {str(excitation.excitations_generators): [x*x for x in excitation_gen_matrices]}
    global_cache = Cache(None, 6, 3, exc_gen_sparse_matrices_dict=exc_gen_sparse_matrices_dict,
                         sqr_exc_gen_sparse_matrices_dict=sqr_exc_gen_sparse_matrices_dict)

    statevector_3 = numpy.array(global_cache.get_statevector([excitation], [parameter]).todense())

    print(statevector_0)
    print(statevector_1)