예제 #1
0
    def statevector_from_ansatz(ansatz,
                                var_parameters,
                                n_qubits,
                                n_electrons,
                                init_state_qasm=None):
        assert n_electrons < n_qubits
        qasm = ['']
        qasm.append(QasmUtils.qasm_header(n_qubits))

        # initial state
        if init_state_qasm is None:
            qasm.append(QasmUtils.hf_state(n_electrons))
        else:
            qasm.append(init_state_qasm)

        ansatz_qasm = QiskitSimBackend.qasm_from_ansatz(ansatz, var_parameters)
        qasm += ansatz_qasm

        # Get a circuit of SWAP gates to reverse the order of qubits. This is required in order the statevector to
        # match the reversed order of qubits used by openfermion when obtaining the Hamiltonian Matrix.
        qasm.append(QasmUtils.reverse_qubits_qasm(n_qubits))

        qasm = ''.join(qasm)
        statevector = QiskitSimBackend.statevector_from_qasm(qasm)
        # print(qasm)
        return statevector
예제 #2
0
    def elements_individual_vqe_energy_reductions(vqe_runner, ansatz_elements, elements_parameters=None, ansatz=None,
                                                  ansatz_parameters=None, excited_state=0, global_cache=None):

        if ansatz is None:
            ansatz = []
            ansatz_parameters = []

        # TODO this will work only if the ansatz element has 1 var. par.
        if elements_parameters is None:
            elements_parameters = list(numpy.zeros(len(ansatz_elements)))

        if vqe_runner.backend == backends.QiskitSimBackend:
            ansatz_qasm = QasmUtils.hf_state(vqe_runner.q_system.n_electrons)
            ansatz_qasm += vqe_runner.backend.qasm_from_ansatz(ansatz, ansatz_parameters)
        else:
            ansatz_qasm = None

        def get_thread_cache(element):
            if global_cache is not None:
                init_sparse_statevector = global_cache.get_statevector(ansatz, ansatz_parameters)
                return global_cache.single_par_vqe_thread_cache(element, init_sparse_statevector)
            else:
                return None

        if config.multithread:
            elements_results = []
            chunk_size = config.multithread_chunk_size
            if chunk_size is None:
                chunk_size = len(ansatz_elements)
            n_chunks = int(len(ansatz_elements) / chunk_size) + 1
            for i in range(n_chunks):
                # logging.info('Calculating commutators, patch No: {}'.format(i))
                ansatz_elements_chunk = ansatz_elements[i * chunk_size:][:chunk_size]

                ray.init(num_cpus=config.ray_options['n_cpus'], object_store_memory=config.ray_options['object_store_memory'])
                elements_ray_ids = [
                    [element,
                     vqe_runner.vqe_run_multithread.remote(self=vqe_runner, ansatz=[element], init_state_qasm=ansatz_qasm,
                                                           init_guess_parameters=[elements_parameters[i]],
                                                           excited_state=excited_state, cache=get_thread_cache(element))
                     ]
                    # TODO this will work only if the ansatz element has 1 var. par.
                    for i, element in enumerate(ansatz_elements_chunk)
                ]
                elements_results += [[element_ray_id[0], ray.get(element_ray_id[1])] for element_ray_id in
                                     elements_ray_ids]
                ray.shutdown()
        else:
            # use thread cache even if not multithreading since it contains the precalculated init_sparse_statevector
            elements_results = [
                [element, vqe_runner.vqe_run(ansatz=[element], init_guess_parameters=[elements_parameters[i]],
                                             excited_state=excited_state, init_state_qasm=ansatz_qasm,
                                             cache=get_thread_cache(element))
                 ]
                for i, element in enumerate(ansatz_elements)
            ]

        return elements_results
예제 #3
0
    def test_hf_states(self):
        n_qubits = 5
        n_electrons = 3

        qasm = QasmUtils.qasm_header(n_qubits)
        qasm += QasmUtils.hf_state(n_electrons)
        qasm += QasmUtils.reverse_qubits_qasm(n_qubits)
        qiskit_statevector = QiskitSimBackend.statevector_from_qasm(qasm)

        sparse_statevector = scipy.sparse.csr_matrix(
            openfermion.utils.jw_hartree_fock_state(n_electrons, n_qubits))
        array_statevector = numpy.array(sparse_statevector.todense())[0]

        for i in range(len(qiskit_statevector)):
            self.assertEqual(qiskit_statevector[i], array_statevector[i])