Creating Qubit Permutations 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 how to create qubit permutations on IBM’s quantum devices using the Permutation function in Qiskit.

What is a Qubit Permutation?

A qubit permutation is where qubit states are transferred to another qubit with the use of SWAP gates. For example consider 3 qubits. A permutation could be created where qubit 0 is mapped to qubit 1, qubit 1 is mapped to qubit 2 and qubit 2 is mapped to qubit 0.

Implementation

In Qiskit qubit permutations can by added using the permutation function:

Permutation(num_qubits, pattern=None, seed=None)

Where:

  • num_qubits: is the number of qubits you want to permute

  • pattern: is your permutation order

  • seed: is the seed for the random permutation if no pattern is given

To create a permutation order the pattern has to be a list of ordered integers. For example consider we have 3 qubits our map could be [2,1,0].

This would mean that qubit 0 will be mapped to 2, qubit 1 will be mapped to qubit 1 and qubit 0 will be mapped to qubit 2.

Note that if you don’t include a pattern then the function will generate a random permutation. As such you can set the random seed.

The below code generates 2 examples. The first is an 8 qubit circuit that generates a permutation using the map [7,0,6,1,5,2,4,3].

This will map the following:

  • Qubit 0 to Qubit 7

  • Qubit 1 to Qubit 0

  • Qubit 2 to Qubit 6

  • Qubit 3 to Qubit 1

  • Qubit 4 to Qubit 5

  • Qubit 5 to Qubit 2

  • Qubit 6 to Qubit 4

  • Qubit 7 to Qubit 3

To see the permutation happen qubits 0-3 are initialised to 1 using a Pauli X gate.

The second example does not include an actual pattern. As such a random pattern is generated.

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 import QuantumCircuit, execute,IBMQ
from qiskit.circuit.library import Permutation
from qiskit.tools.monitor import job_monitor

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

backend = provider.get_backend('ibmq_qasm_simulator')


q = QuantumRegister(8, 'q')
c = ClassicalRegister(8, 'c')

########### PERMUTATION WITH PATTERN 7,0,6,1,5,2,4,3 
circuit = QuantumCircuit(q, c)

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

circuit += Permutation(num_qubits = 8, pattern = [7,0,6,1,5,2,4,3])

circuit.measure(q, c)

job = execute(circuit, backend, shots=100)
job_monitor(job)

counts = job.result().get_counts()

print(circuit)
print(counts)

####### RANDOM PERMUTATION CIRCUIT 
circuit = QuantumCircuit(q, c)

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

circuit += Permutation(num_qubits = 8)

circuit.measure(q, c)

job = execute(circuit, backend, shots=100)
job_monitor(job)

counts = job.result().get_counts()

print(circuit)
print(counts)

Output

Output of the code showing a randomly generated permutation circuit

Output of the code showing a randomly generated permutation circuit