# Built-in macros in blueqat 0.4.7 (or later)

We released blueqat ver. 0.4.7 and implemented Gray-code based multi-controlled gates.

# Update to latest version of blueqat
!pip install -U blueqat

## Activate built-in macros

To activate macros, execute import blueqat.macros.

from blueqat import Circuit
import blueqat.macros

Now, built-in macros are available.

## Draw the circuit

We add new macros for drawing the circuit. This feature requires qiskit.

Circuit().h.cx[0, 1].draw()
# In jupyter notebook, output='mpl' generates beautiful drawing.
# You may needs to run pip install pylatexenc before.
Circuit().h.cx[0, 1].draw(output='mpl')

## Multi-controlled gate

blueqat has ccx and ccz, but doesn't have multi-controlled gate. Therefore, we implemented them as macros.

### Multi-controlled X, Z

c3x, c4x, c3z, c4z are available.

Circuit.c3x(c0, c1, c2, t): 3-controlled X
Circuit.c3z(c0, c1, c2, t): 3-controlled Z
Circuit.c4x(c0, c1, c2, c3, t): 4-controlled X
Circuit.c4z(c0, c1, c2, c3, t): 4-controlled Z

for c0 in [0, 1]:
for c1 in [0, 1]:
for c2 in [0, 1]:
for c3 in [0, 1]:
c = Circuit()
if c0:
c.x
if c1:
c.x
if c2:
c.x
if c3:
c.x
c.c4x(0, 1, 2, 3, 4)
result = c.m[:].shots(10)
print(f'C4x |{c0}{c1}{c2}{c3}0>')
print(f'Expected: {c0}{c1}{c2}{c3}{c0*c1*c2*c3}, Actual:', result)

It seems OK.

We also prepared arbitrary number of control gates, mcx_gray and mcz_gray.

Circuit.mcx_gray([controls], target): Multi-controlled X Circuit.mcz_gray([controls], target): Multi-controlled Z

c0, c1, c2 = 1, 1, 1
for c3 in [0, 1]:
for c4 in [0, 1]:
c = Circuit().x[0, 1, 2]
if c3:
c.x
if c4:
c.x
c.mcx_gray([0, 1, 2, 3, 4], 5)
result = c.m[:].shots(10)
print(f'C4x |{c0}{c1}{c2}{c3}{c4}0>')
print(f'Expected: {c0}{c1}{c2}{c3}{c4}{c0*c1*c2*c3*c4}, Actual:', result)

Please note that the number of gates is grown exponentially by the number of control qubits.

Circuit().mcx_gray([0, 1, 2], 3).draw(output='mpl')
Circuit().mcx_gray([0, 1, 2, 3], 4).draw(output='mpl')
Circuit().mcx_gray([0, 1, 2, 3, 4], 5).draw(output='mpl')

### Multi-controlled RX, RY, RZ, R

Multi-controlled rotation macros are also available.
R-gate and RZ-gate are same except global phase. However, controlled-R and controlled-RZ are different gate.

mcrx_gray(theta, [controls], target): Multi-controlled RX(theta)
mcry_gray(theta, [controls], target): Multi-controlled RY(theta)
mcrz_gray(theta, [controls], target): Multi-controlled RZ(theta)
mcr_gray(theta, [controls], target): Multi-controlled R(theta)

from math import pi

for c0 in [0, 1]:
for c1 in [0, 1]:
for c2 in [0, 1]:
c = Circuit()
if c0:
c.x
if c1:
c.x
if c2:
c.x
c.mcry_gray(pi / 2, [0, 1, 2], 3)
if c0 * c1 * c2 == 0:
print(f"Expect: {c0}{c1}{c2}0: 100%")
else:
print(f"Expect: {c0}{c1}{c2}0: 50%, {c0}{c1}{c2}1: 50%")
print("Actual:", c.m.shots(100))

### Multi-controlled U

U-gate can represent arbitrary 2x2 unitary gate with 4 parameters theta, phi, lambda and gamma.
We implemented mcu_gray macro.

mcu_gray(theta, phi, lam, gamma, [controls], target): Multi-controlled U

from blueqat.circuit_funcs import circuit_to_unitary
c = Circuit().mcu_gray(pi / 2, pi / 4, pi / 8, pi / 16, [0, 1, 2], 3)
mcu = circuit_to_unitary(c)
c.draw(output='mpl')
import numpy as np
u = circuit_to_unitary(Circuit().u(pi / 2, pi / 4, pi / 8, pi / 16,))
expected = np.eye(16, dtype=complex)
expected[7, 7] = u[0, 0]
expected[15, 7] = u[1, 0]
expected[7, 15] = u[0, 1]
expected[15, 15] = u[1, 1]

np.allclose(mcu, expected)