Skip to content

Classiq code for discrete quantum walk

View on GitHub

This notebook shows how to generate data for discrete quantum walk using classiq.

import time

from classiq import *

SIZE = 6
MAX_WIDTH = 40
constraints = Constraints(optimization_parameter="cx", max_width=MAX_WIDTH)
# mcx from control on X


@qfunc
def my_mcx(x: QNum, y: QBit):
    control(x == 2**x.size - 1, lambda: X(y))


# define increment circuit as an MCX cascade
@qfunc
def increment(x: QArray[QBit]):
    repeat(x.len - 1, lambda i: my_mcx(x[0 : x.len - 1 - i], x[x.len - 1 - i]))
    X(x[0])


@qfunc
def single_step_walk(
    x: QNum,  # position
):
    coin = QBit()
    allocate(coin)
    H(coin)
    control(coin == 0, lambda: increment(x), lambda: invert(lambda: increment(x))),
from classiq import CustomHardwareSettings, Preferences

preferences = Preferences(
    custom_hardware_settings=CustomHardwareSettings(basis_gates=["cx", "u"]),
    transpilation_option="custom",
    debug_mode=False,
)

Example for getting a data point

start_time = time.time()


@qfunc
def main(x: Output[QNum[SIZE, UNSIGNED, 0]]):
    allocate(x)
    single_step_walk(x)


qmod = create_model(main, constraints=constraints, preferences=preferences)

write_qmod(qmod, "quantum_walk_classiq")
qprog = synthesize(qmod)
compilation_time = time.time() - start_time

width = qprog.data.width
depth = qprog.transpiled_circuit.depth
cx_counts = qprog.transpiled_circuit.count_ops["cx"]

print(f"==== classiq for {SIZE}==== time {compilation_time}")
==== classiq for 6==== time 15.563591003417969