blueqat to extract quantum circuits in json
See how to use the json module to extract the quantum circuit in json and connect it to the front GUI. The function is prepared.
Enclose the quantum circuit in json_serializer.serialize().
from blueqat import Circuit
from blueqat.circuit_funcs import json_serializer
json_serializer.serialize(Circuit().h[0].cx[0,1].cx[1,2].cx[2,3].h[4])
{'schema': {'name': 'blueqat-circuit', 'version': '1'},
'n_qubits': 5,
'ops': [{'name': 'h', 'params': [], 'targets': [0]},
{'name': 'cx', 'params': [], 'targets': [0, 1]},
{'name': 'cx', 'params': [], 'targets': [1, 2]},
{'name': 'cx', 'params': [], 'targets': [2, 3]},
{'name': 'h', 'params': [], 'targets': [4]}]}
In the above circuit, each of the circuits for each quantum gate has been broken down into json.
Next, I would like to json the circuit for QAOA combinatorial optimization using the QAOA module.
First, we will create a normal QAOA from a Hamiltonian.
from blueqat.pauli import Z
from blueqat import vqe
hamiltonian = Z[0]*Z[1] + Z[1]*Z[2] + Z[2]*Z[0]
result = vqe.Vqe(vqe.QaoaAnsatz(hamiltonian, 1)).run()
print(result.most_common(12))
(((1, 0, 0), 0.1657125301530595), ((0, 1, 0), 0.1657125301530595), ((1, 1, 0), 0.1657125301530595), ((0, 0, 1), 0.1657125301530595), ((1, 0, 1), 0.1657125301530595), ((0, 1, 1), 0.1657125301530595), ((0, 0, 0), 0.0028624095408214395), ((1, 1, 1), 0.0028624095408214395))
And to json, you can try using this circuit
json_serializer.serialize(result.circuit)
{'schema': {'name': 'blueqat-circuit', 'version': '1'},
'n_qubits': 3,
'ops': [{'name': 'h', 'params': [], 'targets': [0]},
{'name': 'h', 'params': [], 'targets': [1]},
{'name': 'h', 'params': [], 'targets': [2]},
{'name': 'cx', 'params': [], 'targets': [0, 1]},
{'name': 'rz', 'params': [-18.140183439785417], 'targets': [1]},
{'name': 'cx', 'params': [], 'targets': [0, 1]},
{'name': 'cx', 'params': [], 'targets': [0, 2]},
{'name': 'rz', 'params': [-18.140183439785417], 'targets': [2]},
{'name': 'cx', 'params': [], 'targets': [0, 2]},
{'name': 'cx', 'params': [], 'targets': [1, 2]},
{'name': 'rz', 'params': [-18.140183439785417], 'targets': [2]},
{'name': 'cx', 'params': [], 'targets': [1, 2]},
{'name': 'rx', 'params': [-13.149159349796419], 'targets': [0]},
{'name': 'rx', 'params': [-13.149159349796419], 'targets': [1]},
{'name': 'rx', 'params': [-13.149159349796419], 'targets': [2]}]}