Closed kharazity closed 2 years ago
Also, it would be really nice, if one could also control how the unitary layers get "black boxed" when printing the circuit diagram. For example, the current output for a 4 layer 4 qubit circuit is:
But each one of those 4 qubit layers are built of one and two qubit unitaries, how can I have it display the black-boxed one and 2 qubit unitaries U_circ
as found in build_layer
instead of the large black box over the entire "layer" formed at build_circuit
? It's confusing because it seems like decompose()
ought to do this, but it doesn't display the circuit, just the black box.
Hi @kharazity. I have taken a brief look at your code, without really trying to understand what it does.
Each layer_circ
, created by layer_circ = build_layer(layer_dict, Nq)
, is a subcircuit with 4 quantum and 4 classical registers. The total circuit total_circ
is also a quantum circuit with 4 quantum and 4 classical registers. So you want to change the build_circuit
to something like this:
def build_circuit(mpo):
layer_list = build_unitary_layers(mpo)
Nq = len(mpo[0])
total_circ = QuantumCircuit(Nq, Nq)
qr = total_circ.qregs[0]
cr = total_circ.cregs[0]
for layer_dict in layer_list:
layer_circ = build_layer(layer_dict, Nq)
total_circ.append(layer_circ.decompose(), [qr[i] for i in range(Nq)], [cr[i] for i in range(Nq)])
return total_circ.decompose()
Note that append
takes both the list of quantum registers and the list of classical registers. Then everything works fine.
I actually did not understand why you need classical registers at all, especially for layer_circ
. So another possible fix is to let qc
in build_layer
be defined as qc = QuantumCircuit(Nq)
(with your original build_circuit
).
Hope this helps.
I also did not understand why you call decompose on layer circuits, especially in view of your second question. Try this:
def build_layer(unitary_layer, Nq):
qc = QuantumCircuit(Nq)
qr = qc.qregs[0]
for key in unitary_layer.keys():
unitary = unitary_layer[key]
qubits = list(key)
U_circ = decompose_unitary(unitary)
qc.append(U_circ, [qr[i] for i in qubits])
qc = transpile(qc,optimization_level=3)
return qc
def build_circuit(mpo):
layer_list = build_unitary_layers(mpo)
Nq = len(mpo[0])
total_circ = QuantumCircuit(Nq, Nq)
qr = total_circ.qregs[0]
for layer_dict in layer_list:
layer_circ = build_layer(layer_dict, Nq)
total_circ.append(layer_circ, [qr[i] for i in range(Nq)])
return total_circ
if __name__ == "__main__":
mpo = np.load('mpo_data.npy',allow_pickle=True)
qc = build_circuit(mpo)
print(qc)
print(qc.decompose())
print(qc.decompose().decompose())
Hmm, also appending transpiled subcircuits seems strange (as transpilation may change the order of the wires), so you probably also want to call transpile only on the final circuit.
Hey @alexanderivrii thanks so much! I'm a bit of a qiskit newbie, these were very helpful suggestions and now the program outputs as desired.
Follow up question, is there any advantage to "local" circuit optimization prior to "global" circuit optimization? I.e. is it worth attempting to perform a qubit order preserving transformation on each layer to simplify the layer before then running a full gate-level optimization on the entire circuit?
@kharazity, I don't think there is any advantage doing "local" optimizations first.
Since this is not a bug, we can close this issue. @kharazity, please do not hesitate to ask more questions.
Environment
What is happening?
I have compiled a circuit from a list of dictionaries of one and two-qubit unitary matrices. Each list corresponds to a "layer", and each element of the layer is either a one or two-qubit unitary. Each one and two qubit unitary are compiled to a gateset via
OneQubitEulerDecomposer()(unitary)
andTwoQubitWeylDecomposition(unitary).circuit()
respectively into a circuit for the whole layer. Then the entire circuit is built from appending each layer. However, when I attempt todecompose()
the final circuit so I can view it's structure I get an error from the DAG saying it received 2x as many wires as it was supposed to, even thoughqc.num_qubits = 4
:How can we reproduce the issue?
mpo_data.zip
What should happen?
It should ideally just compile the circuit to a more optimized form, or at the very least spit out the one and two-qubit representation of the system.
Any suggestions?
No response