Repository: Qiskit/qiskit

Issue:
A bug in circuit initialization
<!--- Provide a general summary of the issue in the Title above -->

Providing incorrect number of arguments.
Here: https://github.com/QISKit/qiskit-sdk-py/blob/master/qiskit/extensions/quantum_initializer/_initializer.py#L103

The method `circ.initialize` only takes 2 arguments but 3 is given. (https://github.com/QISKit/qiskit-sdk-py/blob/master/qiskit/extensions/quantum_initializer/_initializer.py#L438)

## Possible Solution
change that line to `self._modifiers(circ.initialize(self.param, self.arg))`



Discussion:

Resolving patch:
diff --git a/qiskit/extensions/quantum_initializer/_initializer.py b/qiskit/extensions/quantum_initializer/_initializer.py
--- a/qiskit/extensions/quantum_initializer/_initializer.py
+++ b/qiskit/extensions/quantum_initializer/_initializer.py
@@ -100,7 +100,7 @@ def nth_qubit_from_least_sig_qubit(self, nth):
 
     def reapply(self, circ):
         """Reapply this gate to the corresponding qubits in circ."""
-        self._modifiers(circ.initialize(self.name, self.param, self.arg))
+        self._modifiers(circ.initialize(self.param, self.arg))
 
     def gates_to_uncompute(self):
         """


Test patch:
diff --git a/test/python/test_initializer.py b/test/python/test_initializer.py
--- a/test/python/test_initializer.py
+++ b/test/python/test_initializer.py
@@ -23,10 +23,10 @@
 import unittest
 
 from qiskit import QISKitError
-from qiskit import QuantumProgram
 from qiskit import QuantumCircuit
 from qiskit import QuantumRegister
 from qiskit import ClassicalRegister
+from qiskit import wrapper
 from qiskit.tools.qi.qi import state_fidelity
 from .common import QiskitTestCase
 
@@ -38,12 +38,10 @@ class TestInitialize(QiskitTestCase):
 
     def test_uniform_superposition(self):
         desired_vector = [0.5, 0.5, 0.5, 0.5]
-        qp = QuantumProgram()
         qr = QuantumRegister(2, "qr")
         qc = QuantumCircuit(qr)
         qc.initialize(desired_vector, [qr[0], qr[1]])
-        qp.add_circuit("qc", qc)
-        result = qp.execute("qc", backend='local_statevector_simulator')
+        result = wrapper.execute(qc, backend_name='local_statevector_simulator')
         statevector = result.get_statevector()
         fidelity = state_fidelity(statevector, desired_vector)
         self.assertGreater(
@@ -52,12 +50,10 @@ def test_uniform_superposition(self):
 
     def test_deterministic_state(self):
         desired_vector = [0, 1, 0, 0]
-        qp = QuantumProgram()
         qr = QuantumRegister(2, "qr")
         qc = QuantumCircuit(qr)
         qc.initialize(desired_vector, [qr[0], qr[1]])
-        qp.add_circuit("qc", qc)
-        result = qp.execute("qc", backend='local_statevector_simulator')
+        result = wrapper.execute(qc, backend_name='local_statevector_simulator')
         statevector = result.get_statevector()
         fidelity = state_fidelity(statevector, desired_vector)
         self.assertGreater(
@@ -66,13 +62,10 @@ def test_deterministic_state(self):
 
     def test_bell_state(self):
         desired_vector = [1/math.sqrt(2), 0, 0, 1/math.sqrt(2)]
-        qp = QuantumProgram()
         qr = QuantumRegister(2, "qr")
         qc = QuantumCircuit(qr)
-        qc = qp.create_circuit("qc", [qr])
         qc.initialize(desired_vector, [qr[0], qr[1]])
-        qp.add_circuit("qc", qc)
-        result = qp.execute("qc", backend='local_statevector_simulator')
+        result = wrapper.execute(qc, backend_name='local_statevector_simulator')
         statevector = result.get_statevector()
         fidelity = state_fidelity(statevector, desired_vector)
         self.assertGreater(
@@ -81,13 +74,10 @@ def test_bell_state(self):
 
     def test_ghz_state(self):
         desired_vector = [1/math.sqrt(2), 0, 0, 0, 0, 0, 0, 1/math.sqrt(2)]
-        qp = QuantumProgram()
         qr = QuantumRegister(3, "qr")
         qc = QuantumCircuit(qr)
-        qc = qp.create_circuit("qc", [qr])
         qc.initialize(desired_vector, [qr[0], qr[1], qr[2]])
-        qp.add_circuit("qc", qc)
-        result = qp.execute("qc", backend='local_statevector_simulator')
+        result = wrapper.execute(qc, backend_name='local_statevector_simulator')
         statevector = result.get_statevector()
         fidelity = state_fidelity(statevector, desired_vector)
         self.assertGreater(
@@ -96,12 +86,10 @@ def test_ghz_state(self):
 
     def test_single_qubit(self):
         desired_vector = [1/math.sqrt(3), math.sqrt(2)/math.sqrt(3)]
-        qp = QuantumProgram()
         qr = QuantumRegister(1, "qr")
         qc = QuantumCircuit(qr)
         qc.initialize(desired_vector, [qr[0]])
-        qp.add_circuit("qc", qc)
-        result = qp.execute("qc", backend='local_statevector_simulator')
+        result = wrapper.execute(qc, backend_name='local_statevector_simulator')
         statevector = result.get_statevector()
         fidelity = state_fidelity(statevector, desired_vector)
         self.assertGreater(
@@ -118,12 +106,10 @@ def test_random_3qubit(self):
             1 / math.sqrt(8) * complex(1, 2),
             1 / math.sqrt(16) * complex(1, 0),
             0]
-        qp = QuantumProgram()
         qr = QuantumRegister(3, "qr")
         qc = QuantumCircuit(qr)
         qc.initialize(desired_vector, [qr[0], qr[1], qr[2]])
-        qp.add_circuit("qc", qc)
-        result = qp.execute("qc", backend='local_statevector_simulator')
+        result = wrapper.execute(qc, backend_name='local_statevector_simulator')
         statevector = result.get_statevector()
         fidelity = state_fidelity(statevector, desired_vector)
         self.assertGreater(
@@ -148,12 +134,10 @@ def test_random_4qubit(self):
             0,
             1 / math.sqrt(4) * complex(1, 0),
             1 / math.sqrt(8) * complex(1, 0)]
-        qp = QuantumProgram()
         qr = QuantumRegister(4, "qr")
         qc = QuantumCircuit(qr)
         qc.initialize(desired_vector, [qr[0], qr[1], qr[2], qr[3]])
-        qp.add_circuit("qc", qc)
-        result = qp.execute("qc", backend='local_statevector_simulator')
+        result = wrapper.execute(qc, backend_name='local_statevector_simulator')
         statevector = result.get_statevector()
         fidelity = state_fidelity(statevector, desired_vector)
         self.assertGreater(
@@ -178,7 +162,6 @@ def test_non_unit_probability(self):
 
     def test_initialize_middle_circuit(self):
         desired_vector = [0.5, 0.5, 0.5, 0.5]
-        qp = QuantumProgram()
         qr = QuantumRegister(2, "qr")
         cr = ClassicalRegister(2, "cr")
         qc = QuantumCircuit(qr, cr)
@@ -188,11 +171,11 @@ def test_initialize_middle_circuit(self):
         qc.reset(qr[1])
         qc.initialize(desired_vector, [qr[0], qr[1]])
         qc.measure(qr, cr)
-        qp.add_circuit("qc", qc)
         # statevector simulator does not support reset
         shots = 2000
         threshold = 0.04 * shots
-        result = qp.execute("qc", backend='local_qasm_simulator', shots=shots)
+        result = wrapper.execute(qc, backend_name='local_qasm_simulator',
+                                 compile_config={'shots': shots})
         counts = result.get_counts()
         target = {'00': shots / 4, '01': shots / 4, '10': shots / 4, '11': shots / 4}
         self.assertDictAlmostEqual(counts, target, threshold)
@@ -215,18 +198,33 @@ def test_sympy(self):
             0,
             1 / math.sqrt(4),
             1 / math.sqrt(4) * complex(0, 1)]
-        qp = QuantumProgram()
         qr = QuantumRegister(4, "qr")
         qc = QuantumCircuit(qr)
         qc.initialize(desired_vector, [qr[0], qr[1], qr[2], qr[3]])
-        qp.add_circuit("qc", qc)
-        result = qp.execute("qc", backend='local_statevector_simulator')
+        result = wrapper.execute(qc, backend_name='local_statevector_simulator')
         statevector = result.get_statevector()
         fidelity = state_fidelity(statevector, desired_vector)
         self.assertGreater(
             fidelity, self._desired_fidelity,
             "Initializer has low fidelity {0:.2g}.".format(fidelity))
 
+    def test_combiner(self):
+        desired_vector = [1, 0]
+        qr = QuantumRegister(1, "qr")
+        cr = ClassicalRegister(1, "cr")
+        qc1 = QuantumCircuit(qr, cr)
+        qc1.initialize([1.0 / math.sqrt(2), 1.0 / math.sqrt(2)], [qr[0]])
+
+        qc2 = QuantumCircuit(qr, cr)
+        qc2.initialize([1.0 / math.sqrt(2), -1.0 / math.sqrt(2)], [qr[0]])
+
+        result = wrapper.execute(qc1+qc2, backend_name='local_statevector_simulator')
+        quantum_state = result.get_statevector()
+        fidelity = state_fidelity(quantum_state, desired_vector)
+        self.assertGreater(
+            fidelity, self._desired_fidelity,
+            "Initializer has low fidelity {0:.2g}.".format(fidelity))
+
 
 if __name__ == '__main__':
     unittest.main()