Applying noise to custom unitary gates#


This notebook shows how to add custom unitary gates to a quantum circuit, and use them for noise simulations in Qiskit Aer.

from qiskit import transpile, QuantumCircuit
import qiskit.quantum_info as qi

from qiskit_aer import AerSimulator
from qiskit_aer.noise import NoiseModel, amplitude_damping_error

from import plot_histogram

Creating matrix operators#

We can use the Operator class in qiskit.quantum_info to represent arbitrary matrix operators. If the operator is unitary it can then be added to a quantum circuit and used for simulation on Qiskit Aer.

Lets create two operators below for a CNOT gate and an iSWAP gate:

\[\begin{split}\mbox{CNOT} = \left(\begin{array} & 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0 \\ 0 & 1 & 0 & 0 \end{array}\right), \quad \mbox{iSWAP} = \left(\begin{array} & 1 & 0 & 0 & 0 \\ 0 & 0 & i & 0 \\ 0 & i & 0 & 0 \\ 0 & 0 & 0 & 1 \end{array}\right)\end{split}\]
# CNOT matrix operator with qubit-0 as control and qubit-1 as target
cx_op = qi.Operator([[1, 0, 0, 0],
                     [0, 0, 0, 1],
                     [0, 0, 1, 0],
                     [0, 1, 0, 0]])

# iSWAP matrix operator
iswap_op = qi.Operator([[1, 0, 0, 0],
                        [0, 0, 1j, 0],
                        [0, 1j, 0, 0],
                        [0, 0, 0, 1]])

Note: The matrix is specified with respect to the tensor product \(U_{b}\otimes U_{a}\) for qubits specified by list [a, b].

Using operators in circuits#

Let us demonstrate how these can be used in a circuit. We will consider an example of implementing a CNOT gate decomposed in terms of single-qubit gates and the iSWAP gate as follows.

# CNOT in terms of iSWAP and single-qubit gates
cx_circ = QuantumCircuit(2, name='cx<iSWAP>')

# Add gates
cx_circ.unitary(iswap_op, [0, 1], label='iswap')
cx_circ.unitary(iswap_op, [0, 1], label='iswap')

     ┌─────┐     ┌────────┐┌─────┐┌───┐┌─────┐┌────────┐
q_0: ┤ SDG ├─────┤0       ├┤ SDG ├┤ H ├┤ SDG ├┤0       ├─────
     ├─────┤┌───┐│  iswap │└─────┘└───┘└─────┘│  iswap │┌───┐
q_1: ┤ SDG ├┤ H ├┤1       ├───────────────────┤1       ├┤ S ├
     └─────┘└───┘└────────┘                   └────────┘└───┘

Note that we have assigned an optional label of "iswap" to the unitary when it is inserted. This allows us to identify this unitary in a Qiskit Aer NoiseModel so that we can add errors to these custom unitary gates in noisy circuit simulations.

We can confirm this circuit returns the correct output using the Operator class as a simulator for the circuit:

# Simulate the unitary for the circuit using Operator:
unitary = qi.Operator(cx_circ)
Operator([[1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
          [0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j],
          [0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j],
          [0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j]],
         input_dims=(2, 2), output_dims=(2, 2))

And to confirm the output is correct we can compute the average gate fidelity:

f_ave = qi.average_gate_fidelity(cx_op, unitary)
print("Average Gate Fidelity: F = {:f}".format(f_ave))
Average Gate Fidelity: F = 1.000000

Creating a custom unitary in a noise model#

The Qiskit Aer AerSimulator supports simulation of arbitrary unitary operators directly as specified by the "unitary" in the basis gates.

'unitary' in AerSimulator().configuration().basis_gates

This allows us to add noise models to arbitrary unitaries in our simulation when we identify them using the optional label argument of QuantumCircuit.unitary.

We will now do this by creating a NoiseModel that includes a quantum error channel on our custom iSWAP gate. For our example we will create a 2-qubit error consisting of two single-qubit amplitude damping channels with different damping parameters. For now we will assume all the other circuit instructions are ideal.

# Error parameters
param_q0 = 0.05  # damping parameter for qubit-0
param_q1 = 0.1   # damping parameter for qubit-1

# Construct the error
qerror_q0 = amplitude_damping_error(param_q0)
qerror_q1 = amplitude_damping_error(param_q1)
iswap_error = qerror_q1.tensor(qerror_q0)

# Build the noise model by adding the error to the "iswap" gate
noise_model = NoiseModel()
noise_model.add_all_qubit_quantum_error(iswap_error, 'iswap')

Note that when we add an error to a custom label such as "iswap" the NoiseModel does not know what gate this label is supposed to apply to, so we must manually add the desired gate string to the noise model basis_gates. This ensures that the compiler will unroll to the correct basis gates for the noise model simulation. This can done using the NoiseModel.add_basis_gates function:

['cx', 'id', 'u3', 'unitary']

By default the basis gates of a noise model are ['cx','id','u3'] plus any standard AerSimulator basis gates that are added to the noise model.

Simulating a custom unitary noise model#

Let us first take our previous CX circuit and add an initial Hadamard gate and final measurement to create a Bell-state preparation circuit that we may simulator on the AerSimulator both for the ideal and noisy case:

# Bell state circuit where iSWAPS should be inserted at barrier locations
bell_circ = QuantumCircuit(2, 2, name='bell')
bell_circ.append(cx_circ, [0, 1])
bell_circ.measure([0,1], [0,1])
q_0: ┤ H ├┤0           ├┤M├───
     └───┘│  cx<iSWAP> │└╥┘┌─┐
q_1: ─────┤1           ├─╫─┤M├
          └────────────┘ ║ └╥┘
c: 2/════════════════════╩══╩═
                         0  1

Ideal output#

Let’s first see the ideal output. Since this generates a Bell-state we expect two peaks for 00 and 11.

# Create ideal simulator backend and transpile circuit
sim_ideal = AerSimulator()
tbell_circ = transpile(bell_circ, sim_ideal)

ideal_result =
ideal_counts = ideal_result.get_counts(0)
               title='Ideal output for iSWAP bell-state preparation')

Noisy circuit execution#

Finally, let’s now simulate it with our custom noise model. Since there is a small amplitude damping error on the two-qubit gates we expect small additional peaks for the 01 and 10 outcome probabilities.

# Create noisy simulator and transpile circuit
sim_noise = AerSimulator(noise_model=noise_model)
tbell_circ_noise = transpile(bell_circ, sim_noise)

# Run on the simulator without noise
noise_result =
noise_counts = noise_result.get_counts(bell_circ)
               title='Noisy output for iSWAP bell-state preparation')

Version Information

Qiskit SoftwareVersion
IBM Q Provider0.12.2
System information
Python3.7.7 (default, May 6 2020, 04:59:01) [Clang 4.0.1 (tags/RELEASE_401/final)]
Memory (Gb)32.0
Fri Apr 02 12:12:41 2021 EDT

This code is a part of Qiskit

© Copyright IBM 2017, 2021.

This code is licensed under the Apache License, Version 2.0. You may
obtain a copy of this license in the LICENSE.txt file in the root directory
of this source tree or at

Any modifications or derivative works of this code must retain this
copyright notice, and modified files need to carry a notice indicating
that they have been altered from the originals.

[ ]: