Cirq since 0.10 (you can install it using pip install cirq --pre
) has a function to decompose an arbitrary three qubit unitary cirq.three_qubit_matrix_to_operations
. This, combined with cirq.qasm
can generate 1 and 2 qubit gates in OpenQASM 2.0. Note that this is a pretty verbose decomposition (but at most 20 CZs), as it is based on generic linear algebra decompositions.
import numpy as np
qs = cirq.LineQubit.range(3)
print(cirq.qasm(cirq.Circuit(cirq.three_qubit_matrix_to_operations(qs[0], qs[1], qs[2], 1/2*np.array([
[1,0,0,1,1,0,0,1],
[0,1,1,0,0,1,1,0],
[1,0,0,-1,1,0,0,-1],
[0,1,-1,0,0,1,-1,0],
[0,1,1,0,0,-1,-1,0],
[1,0,0,1,-1,0,0,-1],
[0,-1,1,0,0,1,-1,0],
[-1,0,0,1,1,0,0,-1],
])))))
Which will output:
// Generated from Cirq v0.10.0.dev
OPENQASM 2.0;
include "qelib1.inc";
// Qubits: [0, 1, 2]
qreg q[3];
u2(pi*0.011229858, pi*-0.011229858) q[1];
u3(pi*-0.2503051573, pi*-0.4802949152, pi*0.4802949152) q[2];
rz(pi*-0.5) q[0];
cz q[1],q[2];
u3(pi*-0.4860708536, pi*0.511229858, pi*-0.511229858) q[1];
cz q[1],q[2];
u3(pi*-0.7496948427, pi*0.0057759385, pi*-0.0057759385) q[2];
u2(pi*0.011229858, pi*-0.011229858) q[1];
rz(pi*-0.5254810233) q[2];
rz(pi*0.9972453689) q[1];
cx q[1],q[0];
rz(pi*0.5) q[0];
cx q[2],q[0];
rz(pi*-0.5) q[0];
cx q[1],q[0];
rz(pi*-0.5) q[0];
u2(pi*-1.2086038449, pi*1.2086038449) q[1];
cx q[2],q[0];
u3(pi*-0.4803325646, pi*-0.0197050848, pi*0.0197050848) q[2];
ry(pi*0.5) q[0];
cz q[1],q[2];
rz(pi*-0.5) q[0];
u3(pi*-0.2506091478, pi*1.2913961551, pi*-1.2913961551) q[1];
cz q[1],q[2];
u3(pi*-0.5196674354, pi*-0.2703142327, pi*0.2703142327) q[2];
u2(pi*-1.2086038449, pi*1.2086038449) q[1];
rz(pi*-0.7099806825) q[2];
rz(pi*0.9369127746) q[1];
u2(0, 0) q[1];
u3(pi*-0.2870621217, pi*1.0, pi*-1.0) q[2];
cz q[1],q[2];
u3(pi*-0.2129378783, pi*1.0, pi*-1.0) q[2];
u2(pi*-1.0, pi*1.0) q[1];
cz q[1],q[2];
u2(pi*-1.0, pi*1.0) q[1];
u3(pi*-1.0, pi*1.25, pi*-1.25) q[2];
rz(pi*-0.5) q[1];
cx q[1],q[0];
cx q[2],q[0];
rz(pi*0.5) q[0];
cx q[1],q[0];
cx q[2],q[0];
u2(pi*0.25, pi*-0.25) q[1];
u2(pi*-0.75, pi*0.75) q[2];
cz q[1],q[2];
u3(pi*-0.2129378783, pi*1.25, pi*-1.25) q[2];
u3(pi*-0.2129378783, pi*1.25, pi*-1.25) q[1];
cz q[1],q[2];
u3(pi*-0.25, pi*-0.25, pi*0.25) q[2];
u2(pi*-0.75, pi*0.75) q[1];
rz(pi*-1.25) q[2];