Serialization¶
vqubit can save and load circuits as text, and export them to OpenQASM 2.0.
JSON¶
Every serializable value becomes a JSON object carrying a cirq_type field, and
read_json rebuilds it.
import vqubit as vq
q0, q1 = vq.LineQubit.range(2)
circuit = vq.Circuit(vq.H(q0), vq.CNOT(q0, q1), vq.Rz(0.3)(q1))
text = vq.to_json(circuit)
recovered = vq.read_json(text)
print(recovered == circuit) # True
There are compressed variants too:
You can ask for a value's type name and look a class up by it:
print(vq.json_cirq_type(vq.X)) # 'XPowGate'
print(vq.cirq_type_from_json('LineQubit')) # <class 'vqubit._qubits.LineQubit'>
OpenQASM 2.0¶
import vqubit as vq
q0, q1 = vq.LineQubit.range(2)
circuit = vq.Circuit(vq.H(q0), vq.CNOT(q0, q1), vq.measure(q0, q1, key="m"))
print(vq.qasm(circuit))
It produces:
// Generated by vqubit
OPENQASM 2.0;
include "qelib1.inc";
qreg q[2];
creg m_m[2];
h q[0];
cx q[0],q[1];
measure q[0] -> m_m[0];
measure q[1] -> m_m[1];
Gates without a native instruction (like the Fredkin gate) are expanded through their decomposition before being written.
Quirk¶
quirk_url renders a circuit as a link to the
Quirk simulator, for the gates Quirk understands
natively:
q0, q1 = vq.LineQubit.range(2)
circuit = vq.Circuit(vq.H(q0), vq.CNOT(q0, q1))
print(vq.quirk_url(circuit)) # https://algassert.com/quirk#circuit=...
The QasmArgs object carries the context (qubit names, precision and free
arguments) that a gate's QASM writer receives.
Next steps¶
- Run your circuits on local devices.