pyquil
pyquil copied to clipboard
Defcalibration accepts integers for qubits, but it shouldn't
Pre-Report Checklist
- [x] I am running the latest versions of pyQuil and the Forest SDK
- [x] I checked to make sure that this bug has not already been reported
Issue Description
DefCalibration
correctly type-hints that the qubits should be Qubit
objects, but it works fine if you just provide integers and the generated quil code is identical.
However, this will cause get_calibration
to fail. Why? Because in quiltcalibrations.match_calibration
, there is an equality check. The instruction will produce Qubit objects, while the DefCalibration will produce the integers.
elif cal_field != instr_field:
Proposed Solution
DefCalibration
should attempt to convert to qubit objects. The function also accepts FormalArgument
- I'm not sure what that case is for. If the integers cannot be converted, they should be rejected.
How to Reproduce
Code Snippet
import numpy as np
from pyquil.quilbase import DefGate, DefCalibration
from pyquil.quil import Program
from pyquil.gates import CPHASE, RX
unitary = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, -1.0j]])
defgate = DefGate("sCZ", unitary)
defcal = DefCalibration(
name="sCZ",
parameters=[],
qubits=[5, 6],
instrs=[
CPHASE(np.pi/2, 5,6),
],
)
sCZ = defgate.get_constructor()
program = Program()
program += defgate
program += defcal
program += RX(np.pi/2, 5)
program += RX(np.pi/2, 6)
program += sCZ(5,6)
program.get_calibration(sCZ(5,6))
Environment Context
QCS
To be clear, this behavior only manifests if consumers don't follow the API and provide invalid (as documented) parameter to qubits
, right? So then adding support for integers instead of Qubit
objects would be the feature request?
Yes, that's right. Sorry, maybe it should be labelled as a feature request.
The other solution would be rejecting non-Qubit inputs. It was just confusing because there was no error anywhere.
Also labeled with documentation because we should make it clear how that feature should be used.
This will be addressed in v4, won't do in V3