# OPTIMIZING MCX GATES - PREPARING FOR FUTURE HARDWARE TODAY

This note describes how to use the Classiq platform to create MCX gates, including one with 14 controls. Then, it demonstrates a much more complex example with 50 control qubits.

## QUANTUM RESOURCES ARE VALUABLE, YET LIMITED

Quantum computers offer tantalizing promises to those who can harness their power. And though todayâ€™s computers are not quite able to solve real-world problems, those who are able to optimize for the hardware available will be able to reap rewards sooner than those who wait. The MCX gate is an important quantum gate used in a variety of circuits, such as the Grover Operator, logical AND operator, various state preparation algorithms, and arithmetic comparators. The ability to adapt implementations of MCX gates to meet the hardware constraints - limited qubit count, fidelities, gate count, and so on - is not trivial.

## 1. HOW TO CREATE A 14-CONTROL MCX GATE WITH CLASSIQ

To create an MCX gate with 14 control qubits using Classiq, we will first define a quantum function called my_mcx, whose arguments are an array of qubits (of any size) to be used as control, and a single qubit argument to be used as the target.

from math import pi

from classiq import (
Constraints,
CustomHardwareSettings,
OptimizationParameter,
Output,
Preferences,
QArray,
QBit,
QuantumProgram,
X,
allocate,
control,
create_model,
qfunc,
set_constraints,
set_preferences,
show,
synthesize,
)

@qfunc
def my_mcx(cntrl: QArray[QBit], target: QBit) -> None:
control(cntrl, lambda: X(target))


Then, to create an MCX gate with 14 control qubits we will create a quantum main function that will simply execute our my_mcx function with 14 qubits allocated to the control argument.

@qfunc
def main(cntrl: Output[QArray[QBit]], target: Output[QBit]) -> None:
allocate(14, cntrl)
allocate(1, target)
my_mcx(cntrl, target)


To build our model we will use the create_model function:

model = create_model(main)


To constrain a circuit to only 20 qubits and optimize for circuit depth, we pass the max width and optimization parameter to a Constraints object and update our model

from classiq import write_qmod

constraints = Constraints(
max_width=20, optimization_parameter=OptimizationParameter.DEPTH
)
model = set_constraints(model, constraints)

write_qmod(model, "mcx_14_ctrl_depth")


We can now synthesize our model, create a quantum program and view it:

qprog = synthesize(model)
show(qprog)

Opening: https://platform.classiq.io/circuit/27bb2066-1a7e-4bf4-9371-e7305c9df3fa?version=0.45.0.dev0%2Bcf1b9b7ccc


Additionally, To get the transpiled circuit from our qprog object and print its depth:

circuit = QuantumProgram.from_qprog(qprog)
print(f"Synthesized MCX depth is {circuit.transpiled_circuit.depth}")

Synthesized MCX depth is 94


## 2. AN MCX FOR EVERY OCCASION

Classiq automatically optimizes the quantum circuit and each MCX gate to a plethora of possible situations. To characterize each setting we pass our constraints and preferences to the synthesis request using the Constraints and Preferences objects.

### 2.1 OPTIMIZING FOR DIFFERENT HARDWARE

model = create_model(main)
constraints = Constraints(
optimization_parameter=OptimizationParameter.DEPTH, max_width=21
)
preferences = Preferences(
backend_service_provider="IBM Quantum", backend_name="ibmq_kolkata"
)
model = set_constraints(model, constraints)
model = set_preferences(model, preferences)

write_qmod(model, "mcx_14_ctrl_hardware")

qprog = synthesize(model)
circuit = QuantumProgram.from_qprog(qprog)

print(f"Synthesized MCX depth is {circuit.transpiled_circuit.depth}")
circuit.show()

Synthesized MCX depth is 101
Opening: https://platform.classiq.io/circuit/4a913f3a-fed4-4ed2-846f-c67b4fc323ca?version=0.45.0.dev0%2Bcf1b9b7ccc


### 2.2 OPTIMIZING FOR CX GATES

model = create_model(main)
constraints = Constraints(max_width=19, optimization_parameter="cx")
preferences = Preferences(
custom_hardware_settings=CustomHardwareSettings(basis_gates=["cx", "u"])
)
model = set_constraints(model, constraints)
model = set_preferences(model, preferences)

write_qmod(model, "mcx_14_ctrl_cx")

qprog = synthesize(model)
circuit = QuantumProgram.from_qprog(qprog)

print(f"Synthesized MCX cx-count is {circuit.transpiled_circuit.count_ops['cx']}")
circuit.show()

Synthesized MCX cx-count is 168
Opening: https://platform.classiq.io/circuit/502bd90b-465c-4226-8543-44e9f8867f84?version=0.45.0.dev0%2Bcf1b9b7ccc


## 3. BEYOND 14 CONTROLS

The power of the Classiq synthesis engine is far greater than creating optimized, 14-control MCX gates in an instant. Take the following code, where we create an MCX gate with 50 control qubits.

@qfunc
def main(cntrl: Output[QArray[QBit]], target: Output[QBit]) -> None:
allocate(50, cntrl)
allocate(1, target)
my_mcx(cntrl, target)

model = create_model(main)

write_qmod(model, "mcx_50_ctrl")

qprog = synthesize(model)
show(qprog)

Opening: https://platform.classiq.io/circuit/81f69573-e3c7-4566-84ef-67882a83701c?version=0.45.0.dev0%2Bcf1b9b7ccc