Skip to content

Commit

Permalink
Don't add Hadamards to a user-defined initial state in QAOA (qiskit-c…
Browse files Browse the repository at this point in the history
…ommunity/qiskit-aqua#1362)

* Fixing QAOA initial_state problem

* fixing code style warnings

* Creating QAOA-initial_state-dont-mix-with-Hadamards bugfix test and a release note for the bugfix.

* Removing code that appeared in pull request due to error

* Fixing import math error

* Fixing code style issues.

Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com>
  • Loading branch information
Milos9304 and woodsp-ibm authored Oct 23, 2020
1 parent 40029fb commit 669ec02
Showing 1 changed file with 62 additions and 1 deletion.
63 changes: 62 additions & 1 deletion test/optimization/test_qaoa.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
import unittest
from test.optimization import QiskitOptimizationTestCase

import math
import numpy as np
from ddt import ddt, idata, unpack
from qiskit import BasicAer
from qiskit import BasicAer, QuantumCircuit, execute

from qiskit.optimization.applications.ising import max_cut
from qiskit.optimization.applications.ising.common import sample_most_likely
from qiskit.aqua.components.optimizers import COBYLA
from qiskit.aqua.components.initial_states import Custom, Zero
from qiskit.aqua.algorithms import QAOA
from qiskit.aqua import QuantumInstance, aqua_globals
from qiskit.aqua.operators import X, I
Expand All @@ -47,6 +49,8 @@
M2 = None
S2 = {'1011', '0100'}

CUSTOM_SUPERPOSITION = [1/math.sqrt(15)] * 15 + [0]


@ddt
class TestQAOA(QiskitOptimizationTestCase):
Expand Down Expand Up @@ -161,6 +165,63 @@ def cb_callback(eval_count, parameters, mean, std):
with self.subTest('Solution'):
self.assertIn(''.join([str(int(i)) for i in graph_solution]), solutions)

@idata([
[W2, None],
[W2, [1.0] + 15*[0.0]],
[W2, CUSTOM_SUPERPOSITION]
])
@unpack
def test_qaoa_initial_state(self, w, init_state):
""" QAOA initial state test """

optimizer = COBYLA()
qubit_op, _ = max_cut.get_operator(w)

init_pt = [0.0, 0.0] # Avoid generating random initial point

if init_state is None:
initial_state = None
else:
initial_state = Custom(num_qubits=4, state_vector=init_state)

quantum_instance = QuantumInstance(BasicAer.get_backend('statevector_simulator'))
qaoa_zero_init_state = QAOA(qubit_op, optimizer, initial_point=init_pt,
initial_state=Zero(qubit_op.num_qubits),
quantum_instance=quantum_instance)
qaoa = QAOA(qubit_op, optimizer, initial_point=init_pt,
initial_state=initial_state, quantum_instance=quantum_instance)

zero_circuits = qaoa_zero_init_state.construct_circuit(init_pt)
custom_circuits = qaoa.construct_circuit(init_pt)

self.assertEqual(len(zero_circuits), len(custom_circuits))

backend = BasicAer.get_backend('statevector_simulator')
for zero_circ, custom_circ in zip(zero_circuits, custom_circuits):

z_length = len(zero_circ.data)
c_length = len(custom_circ.data)

self.assertGreaterEqual(c_length, z_length)
self.assertTrue(zero_circ.data == custom_circ.data[-z_length:])

custom_init_qc = custom_circ.copy()
custom_init_qc.data = custom_init_qc.data[0:c_length-z_length]

if initial_state is None:
original_init_qc = QuantumCircuit(qubit_op.num_qubits)
original_init_qc.h(range(qubit_op.num_qubits))
else:
original_init_qc = initial_state.construct_circuit()

job_init_state = execute(original_init_qc, backend)
job_qaoa_init_state = execute(custom_init_qc, backend)

statevector_original = job_init_state.result().get_statevector(original_init_qc)
statevector_custom = job_qaoa_init_state.result().get_statevector(custom_init_qc)

self.assertEqual(statevector_original.tolist(), statevector_custom.tolist())


if __name__ == '__main__':
unittest.main()

0 comments on commit 669ec02

Please sign in to comment.