Skip to content

Commit

Permalink
create an example of modular exponentiation quantum circuit in kubflo…
Browse files Browse the repository at this point in the history
…w pipeline

Signed-off-by: Rhm-B-WT <[email protected]>
  • Loading branch information
Rhm-B-WT committed Jun 24, 2024
1 parent 991a610 commit bc49b46
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
17 changes: 17 additions & 0 deletions samples/contrib/modular_exponentiation_quantum_circuit/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Modular Exponentiation Pipeline

This repository contains a Kubeflow pipeline for executing modular exponentiation using Qiskit. The pipeline is implemented using the `kfp` library and can be compiled into a YAML file for deployment on a Kubeflow Pipelines instance.

## Requirements

To run this pipeline, ensure you have the following installed:

- Python 3.11.7
- Kubeflow Pipelines SDK (`kfp`)

## Pipeline Components
modular_exponentiation Component
This component performs modular exponentiation for a given power number using Qiskit. It returns the quantum circuit diagrams for controlled-U operations for specific values of `a` mod 15.

## modular_exponentiation_pipeline Pipeline
The pipeline defines a single task, modular_exponentiation_task, which executes the modular_exponentiation component with an integer input num.
49 changes: 49 additions & 0 deletions samples/contrib/modular_exponentiation_quantum_circuit/mod_exp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from kfp import dsl
@dsl.component(base_image="python:3.11.7", packages_to_install=['qiskit','matplotlib','pylatexenc'])
def modular_exponentiation(power_num: int) -> dsl.Artifact:
from qiskit import QuantumRegister, QuantumCircuit
def mod_func(a, power, show=False):

assert a in [2,4,7,8,11,13], 'Invalid value of argument a:' + str(a)
qrt = QuantumRegister(4, 'target')
U = QuantumCircuit(qrt)
for i in range(power):
if a in [2,13]:
U.swap(0,1)
U.swap(1,2)
U.swap(2,3)
if a in [7,8]:
U.swap(2,3)
U.swap(1,2)
U.swap(0,1)
if a in [4, 11]:
U.swap(1,3)
U.swap(0,2)
if a in [7,11,13]:
for j in range(4):
U.x(j)
if show:
print('Below is the circuit of U of ' + f'"{a}^{power} mod 15":')
U = U.to_gate()
U.name = f'{a}^{power} mod 15'
C_U = U.control()
return C_U

k = []
power_arg = power_num
for a_arg in [2,4,7,8,11,13]:
qrc = QuantumRegister(1, 'control')
qrt = QuantumRegister(4, 'target')
qc = QuantumCircuit(qrc, qrt)
qc.append(mod_func(a_arg, power_arg, show=True), [0,1,2,3,4])
print('Below is the circuit of controlled U of ' + f'"{a_arg}^{power_arg} mod 15":')
k.append(qc.draw('mpl'))
return k
@dsl.pipeline
def modular_exponentiation_pipeline(num: int):

modular_exponentiation_task = modular_exponentiation(power_num=num)

from kfp import compiler

compiler.Compiler().compile(modular_exponentiation_pipeline, 'modular_exponentiation_pipeline.yaml')

0 comments on commit bc49b46

Please sign in to comment.