예제 #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 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])