Introduction to the MCMT gate in Qiskit with code

Interested in learning how to program quantum computers? Then check out our Qiskit textbook Introduction to Quantum Computing with Qiskit.

Introduction

In this tutorial we will explore the MCMT gate and how to implement it on IBM’s Quantum Computers in Qiskit.

What is the MCMT gate?

The MCMT (Multiple Control Multiple Target) gate is a generalised gate that allows you to add multiple target and control qubits to a range of gate types.

Currently these include:

  • Hadamard gate

  • X gate

  • Y gate

  • Z gate

  • T gate

  • T Diagonal gate

  • S gate

  • S Diagonal gate

Implementation

In Qiskit the MCMT gate is extremely easy to implement as it can be appended to an existing circuit using the MCMT() function.

MCMT(gate,num_ctrl_qubits, num_target_qubits)

Where:

  • gate: Is the type of gate you want to implement

  • num_ctrl_qubits: The number of control qubits

  • num_target_qubits: The number of target qubits

The gate argument must be a string and the following:

  • ‘h’ (or ‘ch’) for the Hadamard gate type

  • ‘x’ (or ‘cx’) for the X gate type

  • ‘y’ (or ‘cy’) for the Y gate type

  • ‘z’ (or ‘cz’) for the Z gate type

  • ‘t’ for the T gate type

  • ‘tdg’ for the T diagonal gate type

  • ‘s’ for the S gate type

  • ‘sdg’ for the S diagnonal gate type

A full code example is in the code section below which shows you how to implement a controlled Hadamard gate consisting of 4 control qubits and 2 target qubits.

How to run the program

Copy and paste the code below in to a python file

  1. Enter your API token in the IBMQ.enable_account('Insert API token here') part

  2. Save and run

Code

from qiskit import QuantumRegister, ClassicalRegister
from qiskit.circuit.library import MCMT
from qiskit import QuantumCircuit, execute,IBMQ
from qiskit.tools.monitor import job_monitor
import numpy as np

IBMQ.enable_account('ENTER API KEY HERE')
provider = IBMQ.get_provider(hub='ibm-q')

backend = provider.get_backend('ibmq_qasm_simulator')

pi = np.pi

q = QuantumRegister(6,'q')
c = ClassicalRegister(2,'c')

circuit = QuantumCircuit(q,c)

#### This circuit shows how to implement a controlled hadamard gate ####
#### consisting of 4 control qubits and 2 target qubits ####

circuit.x(q[0])
circuit.x(q[1])
circuit.x(q[2])
circuit.x(q[3])

circuit += MCMT('h',4,2, label=None)

circuit.measure(q[4],c[0])
circuit.measure(q[5],c[1])

print(circuit)

job = execute(circuit, backend, shots=8192)

job_monitor(job)
counts = job.result().get_counts()

print(counts)

Output

Output showing the measurements from a controlled hadamard gate

Output showing the measurements from a controlled hadamard gate