1
$\begingroup$

I would like to automatically get a phase gate of pi/2 converted to a S gate. I have:

import qiskit as qk
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit,Aer
backend = Aer.get_backend('unitary_simulator')

basis_gates=['h',"cx",'t', "s"]
qreg_q = QuantumRegister(1, 'q')
replacement_circuit = QuantumCircuit(qreg_q)
replacement_circuit.s(0)
qk.circuit.equivalence_library.StandardEquivalenceLibrary.add_equivalence(qk.circuit.library.PhaseGate(np.pi/2),replacement_circuit)

qreg_q = QuantumRegister(1, 'q')
circuit = QuantumCircuit(qreg_q)
circuit.p(np.pi/2,0)
qk.transpile(circuit, backend, basis_gates)

but I get CircuitError: 'Cannot bind parameters (p[0]) not present in the circuit.' Why?

$\endgroup$

1 Answer 1

2
$\begingroup$

If you look at Qiskit's equivalence_library.py, you'll notice that each gate and equivalent circuit include their parameters.

q = QuantumRegister(1, "q")
theta = Parameter("theta")
phase_to_u = QuantumCircuit(q)
phase_to_u.u(0, 0, theta, 0)
_sel.add_equivalence(PhaseGate(theta), phase_to_u)

When BasisTranslator calls the equivalence library, notice that it only passes in the gate name and not matching specific parameters yet.

equiv_lib._get_equivalences((gate_name, gate_num_qubits))

Because you are binding the PhaseGate parameters ahead of time, explains the error telling you the PhaseGate parameter is no longer present.

Instead, since this behavior is tightly coupled with the BasisTranslator pass I might suggest writing a new TransformationPass. For example, iterate through all the gates and make the substitution manually when a PhaseGate(np.pi/2) is found.

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