entropicalabs / openqaoa

Multi-backend SDK for quantum optimisation
MIT License
119 stars 59 forks source link

Avoid measurement_outcomes during optimization #155

Open quosta opened 1 year ago

quosta commented 1 year ago

I like the openQAOA ecosystem and I am trying to integrate a tensor network based backend (using cuQuantum SDK), which does not calculate the explicit wf as statevec. I can use qiskit circuits as starting point before converting them to perform TN contractions, so I adapted a backend from qiskit_sim. However the optimization fails at the first cycle without measurement_outcomes.

Here is the kernel code from the backend, is there a way to bypass measurement_outcomes?

    def expectation(self,
                    params: QAOAVariationalBaseParams) -> float:

        ckt = self.qaoa_circuit(params)

        ## QISKIT VERSION
        # output_wf = Statevector(ckt)
        # self.measurement_outcomes = output_wf.data
        # cost = np.real(output_wf.expectation_value(self.qiskit_cost_hamil))

        ## CUQUANTUM VERSION
        einsum = CircuitToEinsum(ckt, dtype='complex128', backend=cp)
        cost = 0
        for pauli_term_list in self.qiskit_cost_hamil:
            pauli_term = str(pauli_term_list).split(' * ')
            expression, operands = einsum.expectation(pauli_term[1], lightcone=True)
            expec = contract(expression, *operands)
            cost += np.real(expec * float(pauli_term[0]))
        return cost
vishal-ph commented 1 year ago

Hi @quosta, thanks for trying out OpenQAOA; we are glad you like it! Your proposition to add cuQuantum simulator in OpenQAOA sounds quite exciting! So, backends that are non-conventional and do not produce either a wavefunction or some measurement counts as the output of the quantum circuit need to be handled separately. We have been working internally to add one such backend in PR #147. The process I encourage you to follow to add a new backend to OpenQAOA is as follows:

Once you have added and tested your new backend class in OpenQAOA, we can proceed to add it as a supported backend in get_qaoa_backend.

Feel free to ask more questions, and we will be happy to help you!

P.S. One important workflow comment -- The best way to contribute would be to fork the dev branch