Accessing the Qsim Circuit
Is there any way to access the Qsim circuit after translating from the Cirq circuit to the Qsim Circuit?
I'm checking how different the Qsim circuit is concerning the Cirq Circuit. I also wanted to find out the depth of the Translated Qsim circuit.
It is provided that there are some functions under the Class QsimCircuit in qsim_circuit.py. I tried to access the translated circuit, which only returned a circuit object along with its memory address. There are no other functions available to find gate counts and the depth of the circuit.
The code which I have used to access the translated circuit:
import numpy as np num_qubits = 5 s_int = 5 n_iterations = int(np.pi * np.sqrt(2 ** num_qubits) / 4) qc = GroversSearch(num_qubits, s_int, n_iterations) from qsimcirq import QSimCircuit x = QSimCircuit(qc) x.translate_cirq_to_qsim()
The output it returned: Tuple Object (<qsimcirq.qsim_avx2.Circuit at 0x732432cb9870>, [5, 12, 33, 40, 61, 68, 89, 96, 117, 118])
The qsim.Circuit class provided in Python has bindings for accessing its num_qubits and gates properties:
https://github.com/quantumlib/qsim/blob/55b4d0e7ea8f085a1709c2c06ff1e28b3aa93357/pybind_interface/pybind_main.h#L301-L304
You should be able to retrieve these with:
qsim_circuit = x.translate_cirq_to_qsim()
print(qsim_circuit.num_qubits) # prints number of qubits in qsim_circuit
print(len(qsim_circuit.gates)) # prints number of gates in qsim_circuit
Note that these bindings are only for CPU implementations of qsim; the GPU implementation does not have these bindings.
For circuit depth, you can use the output you printed above. QSimCircuit.translate_cirq_to_qsim returns:
https://github.com/quantumlib/qsim/blob/55b4d0e7ea8f085a1709c2c06ff1e28b3aa93357/qsimcirq/qsim_circuit.py#L391-L392
So the length of [5, 12, 33, 40, 61, 68, 89, 96, 117, 118] is the number of moments in the circuit (i.e. its depth).
moment boundary gate indices
Thanks for your help. I have now received clarification regarding how to find the depth of the translated QSIM circuit. But one thing bothers me. When I used print(len(qsim_circuit.gates)), I received the following error. Do you have any suggestions for this one?!!
TypeError Traceback (most recent call last) TypeError: Unregistered type : qsim::Gate<float, qsim::Cirq::GateKind>
The above exception was the direct cause of the following exception:
TypeError Traceback (most recent call last) Cell In[16], line 4 2 print(ckt) 3 print(ckt.num_qubits) ----> 4 print(len(ckt.gates))
TypeError: Unable to convert function return value to a Python type! The signature was (self: qsimcirq.qsim_avx2.Circuit) -> List[qsim::Gate<float, qsim::Cirq::GateKind>]
Once again, thanks for your previous response!!!
Ah, this appears to be an issue with the python bindings - there is no Gate binding, and apparently the python code can't operate on a list[Gate] without having a binding for Gate.
Fortunately, there is an alternate solution: the "moment boundary gate indices" list that we used to find the circuit depth earlier is a list of the number of gates before each moment boundary. Using the previous list as an example:
[5, 12, 33, 40, 61, 68, 89, 96, 117, 118]
The first moment has 5 gates, the next has 7 (12-5), and so on. The final element of this list (118) is therefore the total number of gates in the translated circuit object. In code:
# Added "moment_boundaries" to pick up the list of moment boundary gate indices
qsim_circuit, moment_boundaries = x.translate_cirq_to_qsim()
print(qsim_circuit.num_qubits) # prints number of qubits in qsim_circuit
print(moment_boundaries[-1]) # prints number of gates in qsim_circuit
@PavanCyborg Thank you for your question. Could you let us know if the alternate solution provided by @95-martin-orion above is sufficient to resolve the issue for you?
Closing due to non-response and because a workaround seems to be available.