Repository: Qiskit/qiskit

Issue:
QuantumCircuit.unitary doesn't accept single integer qargs
<!-- ⚠️ If you do not respect this template, your issue will be closed -->
<!-- ⚠️ Make sure to browse the opened and closed issues -->

### Information

- **Qiskit Terra version**: master @ 251930a
- **Python version**: 3.5
- **Operating system**: osx

### What is the current behavior?

```
>>> import qiskit as qk
>>> qc = qk.QuantumCircuit(1)
>>> qc.x(0)
>>> qc.barrier(0)
>>> qc.unitary([[0,1], [1,0]], 0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/kevin.krsulichibm.com/q/qiskit-terra/qiskit/extensions/unitary.py", line 211, in unitary
    return self.append(UnitaryGate(obj, label=label), qubits, [])
  File "/Users/kevin.krsulichibm.com/q/qiskit-terra/qiskit/circuit/quantumcircuit.py", line 796, in append
    for (qarg, carg) in instruction.broadcast_arguments(expanded_qargs, expanded_cargs):
  File "/Users/kevin.krsulichibm.com/q/qiskit-terra/qiskit/circuit/gate.py", line 212, in broadcast_arguments
    'The amount of qubit/clbit arguments does not match the gate expectation.')
qiskit.circuit.exceptions.CircuitError: 'The amount of qubit/clbit arguments does not match the gate expectation.'
>>> qc.unitary([[0,1], [1,0]], [0])  # This works
```

### Steps to reproduce the problem



### What is the expected behavior?



### Suggested solutions





Discussion:

Resolving patch:
diff --git a/qiskit/extensions/unitary.py b/qiskit/extensions/unitary.py
--- a/qiskit/extensions/unitary.py
+++ b/qiskit/extensions/unitary.py
@@ -19,7 +19,7 @@
 
 from qiskit.circuit import Gate, ControlledGate
 from qiskit.circuit import QuantumCircuit
-from qiskit.circuit import QuantumRegister
+from qiskit.circuit import QuantumRegister, Qubit
 from qiskit.circuit.exceptions import CircuitError
 from qiskit.circuit._utils import _compute_control_matrix
 from qiskit.circuit.library.standard_gates import U3Gate
@@ -213,9 +213,14 @@ def validate_parameter(self, parameter):
 
 def unitary(self, obj, qubits, label=None):
     """Apply unitary gate to q."""
+    gate = UnitaryGate(obj, label=label)
     if isinstance(qubits, QuantumRegister):
         qubits = qubits[:]
-    return self.append(UnitaryGate(obj, label=label), qubits, [])
+    # for single qubit unitary gate, allow an 'int' or a 'list of ints' as qubits.
+    if gate.num_qubits == 1:
+        if isinstance(qubits, (int, Qubit)) or len(qubits) > 1:
+            qubits = [qubits]
+    return self.append(gate, qubits, [])
 
 
 QuantumCircuit.unitary = unitary


Test patch:
diff --git a/test/python/circuit/test_unitary.py b/test/python/circuit/test_unitary.py
--- a/test/python/circuit/test_unitary.py
+++ b/test/python/circuit/test_unitary.py
@@ -145,6 +145,23 @@ def test_3q_unitary(self):
             self.assertIn(qubit.index, [0, 1, 3])
         assert_allclose(dnode.op.to_matrix(), matrix)
 
+    def test_1q_unitary_int_qargs(self):
+        """test single qubit unitary matrix with 'int' and 'list of ints' qubits argument"""
+        sigmax = numpy.array([[0, 1], [1, 0]])
+        sigmaz = numpy.array([[1, 0], [0, -1]])
+        # new syntax
+        qr = QuantumRegister(2)
+        qc = QuantumCircuit(qr)
+        qc.unitary(sigmax, 0)
+        qc.unitary(sigmax, qr[1])
+        qc.unitary(sigmaz, [0, 1])
+        # expected circuit
+        qc_target = QuantumCircuit(qr)
+        qc_target.append(UnitaryGate(sigmax), [0])
+        qc_target.append(UnitaryGate(sigmax), [qr[1]])
+        qc_target.append(UnitaryGate(sigmaz), [[0, 1]])
+        self.assertEqual(qc, qc_target)
+
     def test_qobj_with_unitary_matrix(self):
         """test qobj output with unitary matrix"""
         qr = QuantumRegister(4)