Your t|ket> to Framework Freedom

Published by strangeworks (08/12/2021)

In May we announced a partnership with Cambridge Quantum (CQC). Today, Strangeworks users can use tket\rm t \lvert ket \rangle to take circuits written in one framework and submit them to different simulators or hardware typically reserved for other frameworks. tket\rm t \lvert ket \rangle ​ is helping to bridge the gap across frameworks. In conjunction with Strangeworks facilitating ease of hardware access, the possibilities for experimentation grow exponentially.

In this post, we'll go through a couple of examples that show the power of tket\rm t \lvert ket \rangle ​ running on the Strangeworks platform. First we'll use the tket\rm t \lvert ket \rangle ​ compiler to convert a circuit with gates not available in QASM into a working QASM circuit. Then, we'll show how tket\rm t \lvert ket \rangle ​ can transpile into different frameworks by converting a circuit from Rigetti's Pyquil to IBM's Qiskit and running it on IBM's hosted resources.

Compilation

For this experiment, the goal is to take some gates that aren't normally available in QASM and use the tket\rm t \lvert ket \rangle ​ compiler to create a QASM circuit.

Start by clicking "Run Code" on the code sample linked to this post. After you've cloned the demo to your projects, make sure you're viewing the 'tk-compile.py' file.

c.CRz(0.5, 0, 1)
c.T(2)
c.CSWAP(2, 0, 1)

Here we have a controlled-Rz gate, followed by a T gate, and finishing with a controlled-SWAP gate. None of these gates are available in QASM.

Next, let's compile the circuit into QASM.

pass1 = DecomposeMultiQubitsIBM()
cu = CompilationUnit(c)
pass1.apply(cu)
circ1 = cu.circuit

For fun, let's write it to a QASM file.

qasmfile = 'c2.qasm'
circuit_to_qasm(circ1, qasmfile)

Now we'll use Qiskit to read the qasm file and execute it on the Aer simulator.

qc2 = qiskit.QuantumCircuit.from_qasm_file(qasmfile)
qiskit_backend = strangeworks.qiskit.get_backend("qasm_simulator")
job = qiskit.execute([qc2], qiskit_backend, shots=100)

Finally, click Run Code at the top right and see the result. 

We originally coded three gates, a controlled Z rotation, a T gate, and a controlled SWAP. This circuit looks very different from the one we coded. If you click the QASM tab you can even see the new compiled circuit.

OPENQASM2.0;
include"qelib1.inc";
qregq[3];
cregc[3];
rz(pi/4) q[1];
cx q[0],q[1];
rz(15*pi/4) q[1];
cx q[0],q[1];
cx q[1],q[0];
h q[1];
cx q[0],q[1];
tdg q[1];
t q[2];
cx q[2],q[1];
t q[1];
cx q[0],q[1];
t q[0];
tdg q[1];
cx q[2],q[1];
u2(0,-3*pi/4) q[1];
cx q[2],q[0];
tdg q[0];
t q[2];
cx q[2],q[0];
cx q[1],q[0];
measureq[0] -> c[0];
measureq[1] -> c[1];
measureq[2] -> c[2];

Compiling from Pyquil to Qiskit

Now let's have some real fun by converting a circuit from Rigetti's Pyquil to IBM's Qiskit and running it on IBM's hosted resources

Click 'tket-ibm-hw.py' in the file viewer.

Start with a Bell state in pyquil.

p = pyquil.Program()
p += H(0)
p += CNOT(0, 1)
ro = p.declare('ro', 'BIT', 2)
p += MEASURE(0, ro[0])
p += MEASURE(1, ro[1])

Here's where the power of tket\rm t \lvert ket \rangle ​ comes into play. With one line, we can convert the pyquil circuit into a tket\rm t \lvert ket \rangle ​ circuit. Then we'll run it on the Qiskit Aer simulator.

c = pyquil_to_tk(p)
handle = backend.process_circuit(c, n_shots=2000)

But that's not all. We can then convert the tket\rm t \lvert ket \rangle ​ circuit into qiskit with a single function call and send it to the IBMQ QASM simulator in the cloud.

If you haven't already, make sure you enable IBM hardware access by following the instructions on this page.

qc = tk_to_qiskit(c)
job = qiskit.execute(qc, hw_backend, shots=2000)
result = job.result()

Click 'Run Code' at the top right to see the results.

If you'd like to try executing this example program on real quantum hardware, then uncomment the following line in the project.

#hw_backend = get_backend("ibmq_lima")

Conclusion

This project scratches the surface of what is now possible with tket\rm t \lvert ket \rangle ​ and the Strangeworks platform working together. We look forward to seeing what you'll create with these powerful tools. If you have something cool you'd like to share, send us a link and it could be added to our library to help other users learn this challenging subject together!