Skip to content

Commit

Permalink
Add a helper for qobj compatibility
Browse files Browse the repository at this point in the history
In qutip-v5, a bra ket multiplication returns a number instead of a Qobj. This makes it hard to keep the code clean and compatible for both version. This helper function changes the returned value so that it is identical to the one returned by qutip-v4.
  • Loading branch information
BoxiLi committed Feb 18, 2023
1 parent 14da7f6 commit 8c3af66
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
3 changes: 2 additions & 1 deletion doc/pulse-paper/customize.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
import numpy as np
from qutip import (
fidelity, sigmax, sigmay, sigmaz, basis, qeye, tensor, Qobj, fock_dm)
from qutip_qip.circuit import QubitCircuit, Gate
from qutip_qip.circuit import QubitCircuit
from qutip_qip.operations import Gate
from qutip_qip.device import ModelProcessor, Model
from qutip_qip.compiler import GateCompiler, Instruction
import qutip
Expand Down
6 changes: 3 additions & 3 deletions doc/pulse-paper/dj_algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
OptPulseProcessor, LinearSpinChain, SCQubits, SpinChainModel)
from qutip_qip.circuit import QubitCircuit
from qutip import sigmaz, sigmax, identity, tensor, basis
from qutip_qip.utilities import _qobj_compatibility_helper


# Deutsch-Josza algorithm
dj_circuit = QubitCircuit(num_qubits)
Expand Down Expand Up @@ -79,7 +81,6 @@
ax[i].fill_between([full_tlist[point2], full_tlist[point3]], [vmin ,vmin], [vmax, vmax], color="lightgray", alpha=0.5)
ax[i].vlines([full_tlist[point2], full_tlist[point3]], vmin, vmax, "gray", "--", linewidth=0.8, alpha=0.5)

fig.tight_layout()
fig.savefig("optimal_control_pulse.pdf")
fig.show()

Expand Down Expand Up @@ -124,7 +125,6 @@

full_tlist = scqubits_processor.get_full_tlist()

fig3.tight_layout()
fig3.savefig("transmon_pulse.pdf")
fig3.show()

Expand All @@ -146,7 +146,7 @@
for state in result1.states:
tmp = state.ptrace([0,1])
tmp = basis([2,2], [0,0]).dag() * tmp * basis([2,2], [0,0])
expect.append(np.real(tmp[0, 0]))
expect.append(np.real(_qobj_compatibility_helper(tmp)[0, 0]))

fig5, ax5 = plt.subplots(figsize=(LINEWIDTH, LINEWIDTH*0.7), dpi=200)
ax5.plot(t_record, expect, color="slategrey")
Expand Down
16 changes: 16 additions & 0 deletions src/qutip_qip/utilities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from itertools import chain
from functools import reduce
from packaging.version import parse as parse_version
import numpy as np
import qutip


def _qobj_compatibility_helper(qobj):
"""
Make qobj generated by operations in qutip-v5 identical to the one by qutip-v4.
Here, if `qobj` is a number (e.g. generated by a bra-ket multiplication), it is replaced by a Qobj.
"""
if parse_version(qutip.__version__) >= parse_version("5.dev"):
if np.isscalar(qobj):
return qutip.Qobj([[qobj]], dims=[[1], [1]])
return qobj
7 changes: 3 additions & 4 deletions src/qutip_qip/vqa.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from scipy.optimize import minimize
from scipy.linalg import expm_frechet
from .operations import gate_sequence_product
from .utilities import _qobj_compatibility_helper


class VQA:
Expand Down Expand Up @@ -429,10 +430,8 @@ def cost_derivative(self, U, dU):
dCost = (init.dag() * dU.dag()) * obs * (U * init) + (
init.dag() * U.dag()
) * obs * (dU * init)
if isinstance(dCost, Qobj): # qutip version < 5
return np.real(dCost.full().item())
else: # qutip version >= 5
return np.real(dCost)
dCost = _qobj_compatibility_helper(dCost)
return np.real(dCost.full().item())

def compute_jac(self, angles, indices_to_compute=None):
"""
Expand Down

0 comments on commit 8c3af66

Please sign in to comment.