0
$\begingroup$

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?

$\endgroup$
0
1
$\begingroup$

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
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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