Qiskit わざとNoisyな回路 

参考[Qiskitチュートリアル:Qiskit Aer: Simulators](https://github.com/Qiskit/qiskit-iqx-tutorials/blob/master/qiskit/advanced/aer/1_aer_provider.ipynb)

一般に量子回路では，CNOT数が増えるとノイズ増加するので，Qiskitで確認してみる．

IBMQ ExperienceのNotebook上で実行した．

##準備
必要なライブラリを`import`

```python
%matplotlib inline
# Importing standard Qiskit libraries and configuring account
from qiskit import QuantumCircuit, execute, Aer, IBMQ
from qiskit.compiler import transpile, assemble
from qiskit.tools.jupyter import *
from qiskit.visualization import *
# Loading your IBM Q account(s)
provider = IBMQ.load_account()

from qiskit.providers.aer import noise
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
from qiskit.tools.visualization import plot_histogram
from qiskit.tools.monitor import job_monitor

provider.backends()

device = provider.get_backend('ibmq_essex')
properties = device.properties()
coupling_map = device.configuration().coupling_map
```

##関数の定義

注意: CNOTが連続するとQiskitが勝手に消してくれるので，消されないように`qc.barrier()`を追加.

```python
def qc_noise(nn):
    q = QuantumRegister(2)
    c = ClassicalRegister(2)
    qc = QuantumCircuit(q,c) 
    qc.h( q[0] )
    qc.cx( q[0], q[1] )
    for ii in range(nn):
        #CNOTが連続するとQiskitが勝手に消してくれるので，消されないようにする
        qc.barrier() 
        qc.cx( q[1], q[0] )
    
    qc.measure(q,c)
    
    return qc
```

### 回路の例
`nn = 5`とすると，
<img width="687" alt="noisy_circ.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/164411/0dc8de92-2873-59ac-2cc2-c2336895cbd9.png">
上記のような回路が作られる．

## `noise_model`, `basis_gates`, `simulator`の設定

```python
noise_model = noise.device.basic_device_noise_model(properties)
print(noise_model)

>>NoiseModel:
  Basis gates: ['cx', 'id', 'u2', 'u3']
  Instructions with noise: ['measure', 'id', 'u3', 'cx', 'u2']
  Qubits with noise: [0, 1, 2, 3, 4]
  Specific qubit errors: [('id', [0]), ('id', [1]), ('id', [2]), ('id', [3]), ('id', [4]), ('u2', [0]), ('u2', [1]), ('u2', [2]), ('u2', [3]), ('u2', [4]), ('u3', [0]), ('u3', [1]), ('u3', [2]), ('u3', [3]), ('u3', [4]), ('cx', [0, 1]), ('cx', [1, 0]), ('cx', [1, 2]), ('cx', [1, 3]), ('cx', [2, 1]), ('cx', [3, 1]), ('cx', [3, 4]), ('cx', [4, 3]), ('measure', [0]), ('measure', [1]), ('measure', [2]), ('measure', [3]), ('measure', [4])]
```

```python
# Get the basis gates for the noise model
basis_gates = noise_model.basis_gates

# Select the QasmSimulator from the Aer provider
simulator = Aer.get_backend('qasm_simulator')
```

## 測定結果
```python
result_noise = execute(qc_noise(0), simulator, 
                       noise_model=noise_model,
                       coupling_map=coupling_map,
                       basis_gates=basis_gates).result()
counts_noise = result_noise.get_counts()
plot_histogram(counts_noise, title="Counts for qc_noise(0)")
```
`qc_noise(10)`, `qc_noise(100)`, `qc_noise(500)`で測定してみる．
<img width="455" alt="noisy_circ_00.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/164411/44492d96-3247-7077-ffa2-31d114b49cca.png">
<img width="455" alt="noisy_circ_10.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/164411/7c7a712d-f693-3405-0d68-6e23789ebe82.png">
<img width="455" alt="noisy_circ_100.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/164411/674a061f-5044-a1ab-31e8-b8111478c2ef.png">
<img width="455" alt="noisy_circ_500.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/164411/91c01471-5af0-9118-eb9d-4a0524bb8677.png">

`nn=500`になると，大きく結果が異なってしまう．