Instead of doing this on pen and paper, how to solve the above question using qiskit. Is there any way that qiskit can do all the calculations and display the matrix of unknown gate and if feasible, can also give the name of the gate?
1 Answer
Yes, you can. You should define the quantum gate in a parametric way, set random parameters, execute the circuit and compare the output state with the desired one (using fidelity, for example), then repeat with updated parameters until convergence to the correct output state (fidelity = 1). Here the problem is: how to update the parameters in order to converge to the correct output state as soon as possible (i.e., with few iterations).
Here is some code to start with:
# This code works with qiskit 0.41
from qiskit import QuantumCircuit, transpile, execute, BasicAer
from qiskit.extensions import UnitaryGate
from qiskit.quantum_info import state_fidelity, Statevector
import numpy as np
# matrix of the gate
matrix = np.array([[1, 0, 0, 0],
[0, 0, 1, 0],
[0, 1, 0, 0],
[0, 0, 0, 1]], dtype=complex)
gate = UnitaryGate(matrix)
qc = QuantumCircuit(4)
qc.append(gate, [1, 2])
qc = transpile(qc, optimization_level=3, basis_gates=['u3','cx'])
qc.draw(output='mpl',filename='./circuit.pdf')
backend_sim = BasicAer.get_backend('statevector_simulator')
result = execute(qc, backend_sim).result()
out_state = result.get_statevector(qc)
#print(out_state)
ref_state = Statevector([1.-5.30216531e-16j, 0.+0.00000000e+00j, 0.+0.00000000e+00j, 0.+0.00000000e+00j, 0.+0.00000000e+00j, 0.+0.00000000e+00j, 0.+0.00000000e+00j, 0.+0.00000000e+00j, 0.+0.00000000e+00j, 0.+0.00000000e+00j, 0.+0.00000000e+00j, 0.+0.00000000e+00j, 0.+0.00000000e+00j, 0.+0.00000000e+00j, 0.+0.00000000e+00j, 0.+0.00000000e+00j])
F = state_fidelity(out_state,ref_state)
print(F)
In this example, the reference state is exactly the state computed by the circuit. If you modify the matrix, you will see a different value of fidelity F.
To proceed, you need to define the matrix in a parametric way, define a clever way to update the parameters, and put your code in a loop.
-
$\begingroup$ I am new to qiskit and don't know much. Could you refer to some notebooks where this is implemented? $\endgroup$ Feb 5 at 13:49
-
$\begingroup$ There is no tutorial, but I added a piece of code that should be useful to start. $\endgroup$ Feb 5 at 15:40