Note

Click here to download the full example code

# Quantum Capacity¶

```
import trueq as tq
# make a circuit to assess
circuit = tq.Circuit(
[
{1: tq.Gate.x, (0, 2): tq.Gate.cz},
{0: tq.Gate.h, 1: tq.Gate.h},
{(1, 2): tq.Gate.cnot, (0, 4): tq.Gate.cz},
]
)
# generate a circuit collection to measure the QCAP
circuits = tq.assessments.make_qcap(circuit, [0, 4, 16, 32])
# run the circuits on your hardware or simulator
sim = tq.Simulator().add_depolarizing(0.02).add_overrotation(0.03)
sim.run(circuits)
# generate the QCAP bound
print(tq.assessments.qcap_bound(circuit, circuits))
```

Out:

```
Parameter(qcap, 0.2103, 0.008076, 'Quantum capacity upper bound on the circuit's performance (TVD) under RC')
```

```
# randomly compile the circuit (this code snippet is included to demonstrate
# the relationship between QCAP and randomized compiling)
circuit.measure_all()
rc_circuits = tq.randomly_compile(circuit)
# run the randomly compiled circuit
sim.run(rc_circuits)
# add up the number of times each measurement outcome occurs
rc_results = rc_circuits.sum_results()
# find the total variational distance between the ideal outcome probabilities
# and the number of occurrences of measured bitstrings. This number should be
# below the bound found by qcap_bound above
print(rc_results.tvd(tq.Simulator().state(circuit).probabilities().to_results()))
```

Out:

```
(0.10600000000000004, 0.007948333158593696)
```

**Total running time of the script:** ( 0 minutes 19.862 seconds)