# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Tutorial for AQCEL
# ・Quantum Gate Pattern recognition (level = 1, 2, 3)
# <br>
# ・Circuit Optimization (level = 1, 2)
# <br>

# +
import sys
sys.path.append('..')

import warnings
warnings.simplefilter('ignore')

from qiskit import(QuantumCircuit, QuantumRegister, ClassicalRegister)
# -

# ## Example Circuit

# +
q = QuantumRegister(6, 'q')
c = ClassicalRegister( 6, 'c')
circ = QuantumCircuit(q,c)
circ.toffoli(0,4,1)
circ.toffoli(2,3,0)
circ.x(2)
circ.x(3)
circ.x(2)
circ.x(3)
circ.toffoli(2,3,0)
circ.toffoli(0,4,1)
circ.x(4)
circ.x(1)
circ.toffoli(0,4,1)
circ.toffoli(2,3,0)
circ.h(2)
circ.h(3)
circ.toffoli(2,3,0)
circ.toffoli(0,4,1)
circ.x(5)
circ.cry(1,2,0)
circ.toffoli(1,5,2)
circ.toffoli(0,4,1)
circ.toffoli(2,3,0)
circ.h(2)
circ.h(3)
circ.toffoli(2,3,0)
circ.toffoli(0,4,1)
circ.toffoli(3,4,1)
circ.toffoli(1,5,2)
circ.cry(1,2,0)
circ.toffoli(1,5,2)
circ.toffoli(3,4,1)
circ.x(2)
circ.ry(1,0)
circ.x(2)
circ.toffoli(3,4,1)
circ.toffoli(1,5,2)
circ.cry(2,2,0)
circ.toffoli(1,5,2)
circ.toffoli(3,4,1)
circ.h(1)
circ.cry(2,2,0)
circ.toffoli(1,5,2)
circ.cx(3,4)
circ.cx(1,5)
circ.cry(1,2,0)
circ.cx(1,5)
circ.cx(3,4)
circ.x(2)
circ.cx(1,0)
circ.toffoli(2,3,0)
circ.x(2)
circ.cx(3,4)
circ.cx(1,5)
circ.cry(2,2,0)
circ.cx(1,5)
circ.cx(3,4)
circ.toffoli(0,4,1)
circ.toffoli(2,3,0)
circ.x(2)
circ.x(3)
circ.x(2)
circ.x(3)
circ.toffoli(2,3,0)
circ.toffoli(0,4,1)

for i in range(6):
    circ.measure(i,i)

circ.draw(output='mpl',fold=100)
# -

# ## Pattern recognition

from aqcel.optimization import pattern

example2 = pattern.recognition( circuit=circ, level=2, n_patterns=4, min_num_nodes=4, max_num_nodes=8, min_n_repetition=2)

circ_max, designated_gates2 = example2.quantum_pattern()

circ_max.draw(output='mpl')



# ## Circuit Optimization level1

from aqcel.optimization import slim

print(circ.depth(), ',', circ.__len__())
print('Gate counts:', circ.count_ops())

example1 = slim.circuit_optimization( circuit=circ, threshold=None)
circ_op = example1.slim()
print(circ_op.depth(), ',', circ_op.__len__())
print('Gate counts:', circ_op.count_ops())

circ_op.draw(output='mpl',fold=100)

#  ## Circuit Optimization level 2

from aqcel.optimization import optimization

example3 = optimization.optimizer( circuit=circ, slim_level=2, pattern_level =2, n_patterns=4, min_num_nodes=4, max_num_nodes=8, min_n_repetition=2)

circ_op2 = example3.slimer()

circ_op2.draw(output='mpl',fold=30)

StarcoderdataJupyterScriptsDedupFiltered