2
$\begingroup$

I am experimenting with the UCC module in qiskit nature. From pyscf, I got 1-body and 2-body integrals (I need to use custom integrals) and thus created a mapped Hamiltonian. Then, I create a UCC object and find the optimal parameters, and finally, I visualize the circuit. The code I used (without imports & pyscf-overhead) is shown below.

electronic_energy = ElectronicEnergy.from_raw_integrals(
    ElectronicBasis.MO, one_body_ints, two_body_ints
)
hamiltonian = electronic_energy.second_q_ops()[0]
qubit_converter = QubitConverter(mapper=JordanWignerMapper())
qubit_op = qubit_converter.convert(hamiltonian,num_particles=num_particles)
initial_state = HartreeFock(
        num_spin_orbitals=num_spin_orbitals,
        num_particles=num_particles,
        qubit_converter=qubit_converter,
    )
reps=1
var_form = UCC(
    excitations="sd",
    num_particles=num_particles,
    num_spin_orbitals=num_spin_orbitals,
    initial_state=initial_state,
    qubit_converter=qubit_converter,
    reps=reps,
)
optimizer=COBYLA(maxiter=1000)
vqe = VQE(ansatz=var_form, optimizer=optimizer,quantum_instance=Aer.get_backend("aer_simulator_statevector"))
vqe_result =vqe.compute_minimum_eigenvalue(qubit_op)
from qiskit.tools.visualization import circuit_drawer
circuit=vqe.get_optimal_circuit()
circuit_drawer(circuit, output='mpl', plot_barriers=False)
plt.show()

I thought that I could visualize the full circuit by printing/drawing the circuit object. This is however not the case, all I get is the following:

Not well visualized UCC circuit

How can I visualize the full circuit?

$\endgroup$

1 Answer 1

3
$\begingroup$

You can use QuantumCircuit.decompose() for shallow decomposition

circuit.decompose().draw('mpl')

The result will be enter image description here

For further decomposition you can call decompose() multiple times:

circuit.decompose().decompose().draw('mpl')

or use reps parameter to specify the number of times the circuit should be decomposed:

circuit.decompose(reps = 6).draw('mpl')

Another option is to use Unroller transpiler pass which allows you to specify the target basis gate set:

from qiskit.transpiler.passes import Unroller
from qiskit.converters import circuit_to_dag, dag_to_circuit

# You can specify the target basis gate set:
unroller = Unroller(basis=['u', 'cx'])
unrolled_circuit = dag_to_circuit(unroller.run(circuit_to_dag(circuit)))
unrolled_circuit.draw('mpl')
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.