pennylane-qiskit icon indicating copy to clipboard operation
pennylane-qiskit copied to clipboard

Support `b.num_qubits` for `BackendV2` Qiskit objects

Open JMuff22 opened this issue 2 years ago • 1 comments

My feature request is related to a problem of using Qiskit backends which only support BackendV2 objects and don't support backwards compatibility for BackendV1. This is described in the Qiskit documentation.

The current issue is that when running with such a backend fails at this line due to calling b.configuration().n_qubits , for which the Backend object has no configuration() method. The solution would be to use backend.num_qubits.

The solution is not simple however, because many of the tests and backends support by pennylane-qiskit have the opposite functionality in that they don't support BackendV2 and backend.num_qubits cannot be called. The solution should support both BackendV1 and BackendV2 via backend.configuration().n_qubits and backend.num_qubits.

I tried this solution but I considered it hacky.


    def validate_wires_against_backend(self):
        b = self.backend
        
        try:
            num_qubits = int(b.configuration().n_qubits)
        except AttributeError:
            try:
                num_qubits = int(b.num_qubits)
            except AttributeError:
                raise AttributeError("Neither 'b.configuration().n_qubits' nor 'b.num_qubits' exist.")
                
        if len(self.wires) > num_qubits:
            raise ValueError(f"Backend '{b}' supports a maximum of {num_qubits} wires.")

Thanks!

The particular backend I am trying to use is IQMBackend.

JMuff22 avatar Sep 05 '23 17:09 JMuff22

Hi @JMuff22, thank you very much for opening this feature request! It's very helpful for us to improve the plugin.

By the way, we have a new PennyLane survey. We would love to know your thoughts about PennyLane in order to keep bringing you amazing features :sparkles:.

CatalinaAlbornoz avatar Sep 05 '23 22:09 CatalinaAlbornoz

Whilst some checks were added in https://github.com/PennyLaneAI/pennylane-qiskit/pull/493 . To support all backends we have been using

	def validate_wires_against_backend(self):
		b = self.backend

		try:
			num_qubits = int(b.configuration().n_qubits)
		except AttributeError:
			try:
				num_qubits = int(b.num_qubits)
			except AttributeError:
				raise AttributeError("Neither 'b.configuration().n_qubits' nor 'b.num_qubits' exist.")

		if len(self.wires) > num_qubits:
			raise ValueError(f"Backend '{b}' supports a maximum of {num_qubits} wires.")

to some success for some time now. So b.num_qubits is still needed

JMuff22 avatar Apr 16 '24 07:04 JMuff22

Hi @JMuff22, thank you for your comment! We'll add some improvements to the PennyLane-Qiskit plugin in the next release which should be released in about 3 weeks.

CatalinaAlbornoz avatar Apr 16 '24 18:04 CatalinaAlbornoz