0
$\begingroup$

I have a custom gate in qiskit defined as follows:

sub_circuit = QuantumCircuit(3, name='toggle_cx')
sub_circuit.cx(0,1)
sub_circuit.cx(1,2)
sub_circuit.cx(0,1)
sub_circuit.cx(1,2)

toggle_cx = sub_circuit.to_instruction()

Say I add this to a QuantumCircuit:

qr = QuantumRegister(4)
new_qc = QuantumCircuit(qr)

new_qc.append(toggle_cx, [qr[1],qr[2],qr[3]])

Now, how do I get information about the qiskit-native gates that were used to make up this custom gate from the final QuantumCircuit object? I tried printing the QuantumCircuit object dictionary; it gave the following output:

{'_ancillas': [],
 '_base_name': 'circuit',
 '_calibrations': defaultdict(<class 'dict'>, {}),
 '_clbit_indices': {},
 '_clbits': [],
 '_control_flow_scopes': [],
 '_data': [(Instruction(name='toggle_cx', num_qubits=3, num_clbits=0, params=[]),
            [Qubit(QuantumRegister(4, 'q0'), 1),
             Qubit(QuantumRegister(4, 'q0'), 2),
             Qubit(QuantumRegister(4, 'q0'), 3)],
            [])],
 '_global_phase': 0,
 '_layout': None,
 '_metadata': None,
 '_parameter_table': ParameterTable({}),
 '_parameters': [],
 '_qubit_indices': {Qubit(QuantumRegister(4, 'q0'), 2): BitLocations(index=2, registers=[(QuantumRegister(4, 'q0'), 2)]),
                    Qubit(QuantumRegister(4, 'q0'), 3): BitLocations(index=3, registers=[(QuantumRegister(4, 'q0'), 3)]),
                    Qubit(QuantumRegister(4, 'q0'), 1): BitLocations(index=1, registers=[(QuantumRegister(4, 'q0'), 1)]),
                    Qubit(QuantumRegister(4, 'q0'), 0): BitLocations(index=0, registers=[(QuantumRegister(4, 'q0'), 0)])},
 '_qubits': [Qubit(QuantumRegister(4, 'q0'), 0),
             Qubit(QuantumRegister(4, 'q0'), 1),
             Qubit(QuantumRegister(4, 'q0'), 2),
             Qubit(QuantumRegister(4, 'q0'), 3)],
 'cregs': [],
 'duration': None,
 'name': 'circuit-4',
 'qregs': [QuantumRegister(4, 'q0')],
 'unit': 'dt'}

In this output, it labels the corresponding instruction as toggle_cx and not the qiskit-native gates. I tried to use new_qc.decompose(), however, if my circuit had other gates like Hadamard or PauliX; it will decompose those gates too, into even more basic gates. I only want to see what gates is toggle_cx made up of.

Context: I'm trying to make custom qiskit backends, which will have access only to the QuantumCircuit object passed as a parameter.

$\endgroup$
3

0

Your Answer

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

Browse other questions tagged or ask your own question.