Skip to content

Commit

Permalink
Rewrite the compatibility helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
BoxiLi committed Mar 5, 2023
1 parent 6a60c16 commit 06ec33b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 21 deletions.
4 changes: 2 additions & 2 deletions doc/pulse-paper/dj_algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
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
from qutip_qip.compat import to_scalar


# Deutsch-Josza algorithm
Expand Down Expand Up @@ -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(_qobj_compatibility_helper(tmp)[0, 0]))
expect.append(np.real(to_scalar(tmp)))

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


def to_scalar(qobj_or_scalar):
if isinstance(qobj_or_scalar, qutip.Qobj):
if qobj_or_scalar.dims == [[1], [1]]:
return qobj_or_scalar[0, 0]
return qobj_or_scalar
16 changes: 0 additions & 16 deletions src/qutip_qip/utilities.py

This file was deleted.

6 changes: 3 additions & 3 deletions src/qutip_qip/vqa.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +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
from .compat import to_scalar


class VQA:
Expand Down Expand Up @@ -430,8 +430,8 @@ def cost_derivative(self, U, dU):
dCost = (init.dag() * dU.dag()) * obs * (U * init) + (
init.dag() * U.dag()
) * obs * (dU * init)
dCost = _qobj_compatibility_helper(dCost)
return np.real(dCost.full().item())
dCost = to_scalar(dCost)
return np.real(dCost)

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

0 comments on commit 06ec33b

Please sign in to comment.