3
$\begingroup$

There are a few options already discussed and suggested how to pass the initial state to the QAOA module. I tried all but no one works in my case. Maybe, there are any other ideas?

So, I created the initial state as:

circ = QuantumCircuit(4)
circ.h(0)
circ.h(1)
circ.h(2)
circ.h(3)
backend=Aer.get_backend('statevector_simulator')
job = execute(circ, backend)
result = job.result()
init_state = result.get_statevector(circ)
print(init_state)

then, by passing it to QAOA:

qaoa = QAOA(qubitOp, optimizer, p=3, initial_state=init_state,
                mixer=mixerop, initial_point=init_point,
                callback=store_intermediate_result, quantum_instance=qi)

I got the error:

~\Anaconda3\lib\site-packages\qiskit\aqua\algorithms\minimum_eigen_solvers\qaoa\var_form.py in construct_circuit(self, parameters, q)
     83         # initialize circuit, possibly based on given register/initial state
     84         if self._initial_state is not None:
---> 85             stateVector = CircuitStateFn(self._initial_state.construct_circuit('circuit'))
     86             circuit = stateVector.to_circuit_op()
     87         else:

AttributeError: 'numpy.ndarray' object has no attribute 'construct_circuit'

```
$\endgroup$
2
  • $\begingroup$ Hi, What are the things that you have tried? This will help people to understand the problem better and able to help. Also, take a look at this question quantumcomputing.stackexchange.com/q/15623/9858 Maybe it will be helpful. :) $\endgroup$
    – KAJ226
    Feb 7 at 8:39
  • $\begingroup$ Yes, I tried exactly what are suggested in the answer to this question, but got the errors. $\endgroup$
    – Vlad
    Feb 7 at 9:20
1
$\begingroup$

Try this:

from qiskit.aqua.components.initial_states import Custom
initial_state = Custom(num_qubits , state = 'zero') #if you want to start with zero state

note if you to specify an initial state in equal superposition, you just need to change state = 'uniform' .

And if you want a custom state_vector then you can specify it as:

initial_state = Custom(num_qubits , state_vector = [1,0,0,0,...] ) 

Note that the length of the vector is $2^n$ where $n$ is the number qubits.. so this might not be the most practical way to do thing.


Once you specify all of that, you can just specify it in your QAOA class parameters:

qaoa = QAOA(qubit_op,optimizer, initial_state = initial, quantum_instance=quantum_instance)

Below is a full working script from end to end: (I took the script from one of the Qiskit's tutorial and modified it to have an initial state like you wanted)

import numpy as np
import networkx as nx

from qiskit import BasicAer
from qiskit.aqua.algorithms import NumPyMinimumEigensolver
from qiskit.optimization.applications.ising import graph_partition
from qiskit.optimization.applications.ising.common import random_graph, sample_most_likely

num_nodes = 4
w = random_graph(num_nodes, edge_prob=0.8, weight_range=10, seed=48)
print(w)


G = nx.from_numpy_matrix(w)
layout = nx.random_layout(G, seed=10)
colors = ['r', 'g', 'b', 'y']
nx.draw(G, layout, node_color=colors)
labels = nx.get_edge_attributes(G, 'weight')
nx.draw_networkx_edge_labels(G, pos=layout, edge_labels=labels);


def brute_force():
    # use the brute-force way to generate the oracle
    def bitfield(n, L):
        result = np.binary_repr(n, L)
        return [int(digit) for digit in result]  # [2:] to chop off the "0b" part

    L = num_nodes
    max = 2**L
    minimal_v = np.inf
    for i in range(max):
        cur = bitfield(i, L)

        how_many_nonzero = np.count_nonzero(cur)
        if how_many_nonzero * 2 != L:  # not balanced
            continue

        cur_v = graph_partition.objective_value(np.array(cur), w)
        if cur_v < minimal_v:
            minimal_v = cur_v
    return minimal_v

sol = brute_force()
print(f'Objective value computed by the brute-force method is {sol}')

qubit_op, offset = graph_partition.get_operator(w)


from qiskit.aqua import aqua_globals
from qiskit.aqua.algorithms import QAOA
from qiskit.aqua.components.optimizers import COBYLA
from qiskit.circuit.library import TwoLocal
from qiskit.aqua import QuantumInstance

aqua_globals.random_seed = 10598
quantum_instance = QuantumInstance(BasicAer.get_backend('qasm_simulator'), shots = 10000)    
optimizer = COBYLA(maxiter= 1,tol=0.000000001)  
initial =  Custom(4 , state = 'zero')
qaoa = QAOA(qubit_op,optimizer, initial_state = initial, quantum_instance=quantum_instance)

result = qaoa.compute_minimum_eigenvalue()

x = sample_most_likely(result.eigenstate)
ising_sol = graph_partition.get_graph_solution(x)

print(ising_sol)
print(f'Objective value computed by QAOA is {graph_partition.objective_value(x, w)}')
$\endgroup$
2
  • 1
    $\begingroup$ Now it works, thanks a lot! $\endgroup$
    – Vlad
    Feb 7 at 12:43
  • $\begingroup$ Hope I was able to help. no problem. :) $\endgroup$
    – KAJ226
    Feb 7 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.