Repository: Qiskit/qiskit

Issue:
generated circuits had the same name if circuits are created by multiprocess
<!-- ⚠️ If you do not respect this template, your issue will be closed -->
<!-- ⚠️ Make sure to browse the opened and closed issues -->

### Informations

- **Qiskit Terra version**: the master branch
- **Python version**: 3.7.1
- **Operating system**: macOS 10.13

### What is the current behavior?

The generated circuits have the same name and it results in users cannot retrieve the result correctly since terra uses the circuit name to retrieve the result.

### Steps to reproduce the problem
here is the example script

```python
from qiskit import QuantumRegister, QuantumCircuit, ClassicalRegister
import qiskit as qk
import numpy as np
import concurrent.futures

np.random.seed(0)
# from qiskit_aqua.utils import run_circuits

def generate_circuits(num_qubits, parameters, depth=10):
    q = QuantumRegister(num_qubits, name='q')
    c = ClassicalRegister(num_qubits, name='c')
    circuit = QuantumCircuit(q, c)
    param_idx = 0
    for qubit in range(num_qubits):
        circuit.u3(parameters[param_idx], 0.0, 0.0, q[qubit])
        circuit.u1(parameters[param_idx+1], q[qubit])
        param_idx += 2

    return circuit

num_circuits = 10
num_qubits = 2
depth = 1

num_parameters = num_qubits * (depth + 1) * 2

circuits = []
with concurrent.futures.ProcessPoolExecutor(max_workers=num_circuits) as executor:
    futures = [executor.submit(generate_circuits, num_qubits, np.random.rand(num_parameters), depth) for _ in range(num_circuits)]

    for future in concurrent.futures.as_completed(futures):
        qc = future.result()
        print(qc.name)
        circuits.append(qc)

my_backend = qk.Aer.get_backend('statevector_simulator')
qobj = qk.compile(circuits=circuits, backend=my_backend, seed=0)
qjob = my_backend.run(qobj)
result = qjob.result()

for circuit in circuits:
    print(result.get_statevector(circuit))

```

### What is the expected behavior?

The circuit name should be unique even though they are created via multiprocess

### Suggested solutions

I do not know. The workaround I had now is assigning the name by myself.



Discussion:

Resolving patch:
diff --git a/qiskit/circuit/quantumcircuit.py b/qiskit/circuit/quantumcircuit.py
--- a/qiskit/circuit/quantumcircuit.py
+++ b/qiskit/circuit/quantumcircuit.py
@@ -14,7 +14,8 @@
 from copy import deepcopy
 import itertools
 import warnings
-
+import sys
+import multiprocessing as mp
 
 from qiskit.qasm import _qasm
 from qiskit._qiskiterror import QiskitError
@@ -57,6 +58,11 @@ def __init__(self, *regs, name=None):
         """
         if name is None:
             name = self.cls_prefix() + str(self.cls_instances())
+            # pylint: disable=not-callable
+            # (known pylint bug: https://github.com/PyCQA/pylint/issues/1699)
+            if sys.platform != "win32" and \
+               isinstance(mp.current_process(), mp.context.ForkProcess):
+                name += '-{}'.format(mp.current_process().pid)
         self._increment_instances()
 
         if not isinstance(name, str):


Test patch:
diff --git a/test/python/test_parallel.py b/test/python/test_parallel.py
--- a/test/python/test_parallel.py
+++ b/test/python/test_parallel.py
@@ -9,6 +9,7 @@
 import os
 import time
 
+from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
 from qiskit.transpiler._parallel import parallel_map
 from .common import QiskitTestCase
 
@@ -20,6 +21,13 @@ def _parfunc(x):
     return x
 
 
+def _build_simple(_):
+    qreg = QuantumRegister(2)
+    creg = ClassicalRegister(2)
+    qc = QuantumCircuit(qreg, creg)
+    return qc
+
+
 class TestParallel(QiskitTestCase):
     """A class for testing parallel_map functionality.
     """
@@ -32,3 +40,9 @@ def test_parallel(self):
         """Test parallel_map """
         ans = parallel_map(_parfunc, list(range(10)))
         self.assertEqual(ans, list(range(10)))
+
+    def test_parallel_circuit_names(self):
+        """Verify unique circuit names in parallel"""
+        out_circs = parallel_map(_build_simple, list(range(10)))
+        names = [circ.name for circ in out_circs]
+        self.assertEqual(len(names), len(set(names)))