# Converting 'string' to a function/gate in QisKit

I want to give a list of Quantum gates in a .txt file from user an apply them to a Quantum circuit but the gates in notepad .txt file have 'string' type while in Qiskit I need functions/methods. For example: In .txt file: 'h(0)' In Qiskit: qc=QuantumCircuit(2) qc.h(0) Which is a method. What can I do?

## 1 Answer

You could try mapping the string input to the function using a dictionary somewhat like what is given here.

So considering that your text file has : h(0) as text, you could declare a dictionary in python as : my_functions = {"h(0)":qc.h(0)} to map string input to function

And in the python script, suppose variable x stores the text h[0] which is retrieved from the text file, to apply the h gate on the quantum circuit, you will just need to do:

my_functions[x]


In a nutshell, the code should be:

qc = QuantumCircuit(2, 2) # makes a quantum circuit with 2 qubits
my_functions = {"h(0)":qc.h(0)} # maps the string input to function call
x = "h(0)"
my_functions[x]  ## this will implement h gate on the first qubit