rigetti / pyquil

A Python library for quantum programming using Quil.
http://docs.rigetti.com
Apache License 2.0
1.39k stars 341 forks source link

Defcalibrations affect the resulting wavefunction in pyquil 4 #1704

Closed bramathon closed 3 months ago

bramathon commented 7 months ago

In pyquil 3, the unitary of a defined gate is determined by it's DefGate. However, in pyquil 4, in some cases the unitary is determined by it's Defcal. I believe this is a bug - the DefGate should take precedence.

Code Snippet

import numpy as np
import pyquil
from pyquil.quil import Program
from pyquil.gates import RX
from pyquil.api import WavefunctionSimulator
from pyquil.quilbase import DefCalibration, DefGate
from pyquil.quilatom import Qubit, Parameter, quil_cos, quil_sin
print(f"Pyquil version: {pyquil.__version__}")

phi = Parameter("phi")
mat = np.array(
    [
        [0.0, 1.0],
        [1.0, 0.0],
    ]
)
defgate = DefGate("J", mat, parameters=[])
defcal = DefCalibration("J", parameters=[], qubits=[Qubit(0)], instrs=[RX(np.pi/2, 0)])

program = Program()
program += defgate
program += defcal # comment out this line to see the correct wavefunction
program += defgate.get_constructor()(0)

wf_sim = WavefunctionSimulator()
wavefunction = wf_sim.wavefunction(program)
np.round(wavefunction.amplitudes, 5)

Error Output

Pyquil 3.5.4

Screenshot from 2023-11-28 13-41-16

Pyquil 4.1.1

Screenshot from 2023-11-28 13-38-05