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:
import vqubit as vq
circuit = vq.Circuit(vq.H(vq.LineQubit(0)))
data = vq.to_json_gzip(circuit) # compressed bytes
assert vq.read_json_gzip(data) == circuit
You can ask for a value's type name and look a class up by it:
import vqubit as vq
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[2];
h q[0];
cx q[0],q[1];
measure q[0] -> m[0];
measure q[1] -> 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:
import vqubit as vq
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.
The numpy and sympy contract¶
vqubit does not depend on numpy, but uses it when it is installed, and that
contract is stable:
- Input:
state_vector=,density_matrix=, matrices forMatrixGateandunitary=, and parameters in general accept Python lists as well asnumpyarrays. - Output: with
numpyinstalled,state_vector(),density_matrix(),final_state_vector,density_matrix_of(),unitary()and the channel matrices arenumpyarrays with dtypecomplex128; without it they are nested lists ofcomplexand the rest of the API is unchanged. - Symbolic: parameters are
vq.Parameter(orvq.symbol("t")), notsympy.Symbol: asympysymbol is not accepted by a gate (it raisesTypeErrorinstead of converting silently). Asympy.Symboldoes work as a key of avq.ParamResolver, because it reduces to its name.
Next steps¶
- Run your circuits on local devices.