Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arraylias integration - Rotating frame - #214

Closed
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions docs/tutorials/dynamics_backend.rst
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,9 @@ To enable running of the single qubit experiments, we add the following to the `
backend.
- Add definitions of ``RZ`` gates as phase shifts. These instructions control the phase of the drive
channels, as well as any control channels acting on a given qubit.
- Add a ``CX`` gate which applies to all qubits. While this tutorial will not be utilizing it, this
ensures that validation steps checking that the device is fully connected will pass.
- Add a ``CX`` gate between qubits :math:`(0, 1)` and :math:`(1, 0)`. While this tutorial will not
be utilizing it, this ensures that validation steps checking that the device is fully connected
will pass.

.. jupyter-execute::

Expand All @@ -336,7 +337,7 @@ To enable running of the single qubit experiments, we add the following to the `
target.add_instruction(XGate(), properties={(0,): None, (1,): None})
target.add_instruction(SXGate(), properties={(0,): None, (1,): None})

target.add_instruction(CXGate())
target.add_instruction(CXGate(), properties={(0, 1): None, (1, 0): None})

# Add RZ instruction as phase shift for drag cal
phi = Parameter("phi")
Expand Down Expand Up @@ -472,11 +473,6 @@ values for the single qubit gates calibrated above.

from qiskit_experiments.library import CrossResonanceHamiltonian

backend.target.add_instruction(
instruction=CrossResonanceHamiltonian.CRPulseGate(width=Parameter("width")),
properties={(0, 1): None, (1, 0): None}
)

cr_ham_experiment = CrossResonanceHamiltonian(
qubits=(0, 1),
flat_top_widths=np.linspace(0, 5000, 17),
Expand Down
147 changes: 147 additions & 0 deletions qiskit_dynamics/arraylias_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,161 @@

from typing import Union
from arraylias import numpy_alias
from collections.abc import Iterable
import numpy as np
from scipy.sparse import spmatrix, csr_matrix
from .array import Array
from qiskit.quantum_info.operators import Operator


try:
from jax.experimental.sparse import BCOO
import jax.numpy as jnp
except ImportError:
pass


DYNAMICS_ALIAS = numpy_alias()

# Set qiskit_dynamics.array.Array to be dispatched to numpy
DYNAMICS_ALIAS.register_type(Array, "numpy")

# register required custom versions of functions for sparse type here
DYNAMICS_ALIAS.register_type(spmatrix, lib="scipy_sparse")

# register required custom versions of functions for BCOO type here
DYNAMICS_ALIAS.register_type(BCOO, lib="jax_sparse")

# register required custom versions of functions for Operator type here
DYNAMICS_ALIAS.register_type(Operator, lib="operator")

# register required custom versions of functions for Iterable type here
# need to discuss registering Iterable type because the coverage of Iterable is too broad.
DYNAMICS_ALIAS.register_type(Iterable, lib="iterable")


# asarray
@DYNAMICS_ALIAS.register_function(lib="iterable", path="asarray")
def _(arr):
if isinstance(arr[0], (list, tuple)):
return np.asarray(arr)
return DYNAMICS_ALIAS(like=arr[0]).asarray(
[DYNAMICS_ALIAS().asarray(sub_arr) for sub_arr in arr]
)


@DYNAMICS_ALIAS.register_function(lib="scipy_sparse", path="asarray")
def _(arr):
return np.asarray(arr)


@DYNAMICS_ALIAS.register_function(lib="jax_sparse", path="asarray")
def _(arr):
return jnp.asarray(arr)


@DYNAMICS_ALIAS.register_fallback(path="asarray")
def _(arr):
return np.asarray(arr)


# to_dense
@DYNAMICS_ALIAS.register_default(path="to_dense")
def _(op):
return None


@DYNAMICS_ALIAS.register_function(lib="numpy", path="to_dense")
def _(op):
return op


@DYNAMICS_ALIAS.register_function(lib="jax", path="to_dense")
def _(op):
return op


@DYNAMICS_ALIAS.register_function(lib="scipy_sparse", path="to_dense")
def _(op):
return op.toarray()


@DYNAMICS_ALIAS.register_function(lib="jax_sparse", path="to_dense")
def _(op):
return op.todense()


@DYNAMICS_ALIAS.register_fallback(path="to_dense")
def _(op):
return np.asarray(op)


@DYNAMICS_ALIAS.register_function(lib="iterable", path="to_dense")
def _(op):
return DYNAMICS_ALIAS().asarray([DYNAMICS_ALIAS().to_dense(sub_op) for sub_op in op])


# to_sparse
@DYNAMICS_ALIAS.register_default(path="to_sparse")
def _(op):
return None


@DYNAMICS_ALIAS.register_function(lib="numpy", path="to_sparse")
def _(op):
return csr_matrix(op)


@DYNAMICS_ALIAS.register_function(lib="jax", path="to_sparse")
def _(op):
return BCOO.fromdense(op)


@DYNAMICS_ALIAS.register_function(lib="scipy_sparse", path="to_sparse")
def _(op):
return op


@DYNAMICS_ALIAS.register_function(lib="jax_sparse", path="to_sparse")
def _(op):
return op


@DYNAMICS_ALIAS.register_fallback(path="to_sparse")
def _(op):
return csr_matrix(op)


@DYNAMICS_ALIAS.register_function(lib="iterable", path="to_sparse")
def _(op):
try:
if isinstance(op[0], jnp.ndarray):
return BCOO.fromdense(op)
except ImportError:
return np.array([csr_matrix(sub_op) for sub_op in op])
return np.array([csr_matrix(sub_op) for sub_op in op])


# to_numeric_matrix_type
@DYNAMICS_ALIAS.register_function(lib="iterable", path="to_numeric_matrix_type")
def _(op):
return DYNAMICS_ALIAS().asarray([DYNAMICS_ALIAS().to_sparse(sub_op) for sub_op in op])


@DYNAMICS_ALIAS.register_fallback(path="to_numeric_matrix_type")
def _(op):
return DYNAMICS_ALIAS().asarray(op)


# cond
@DYNAMICS_ALIAS.register_function(lib="numpy", path="cond")
def _(pred, true_fun, false_fun, *operands):
if pred:
return true_fun(*operands)
else:
return false_fun(*operands)


DYNAMICS_NUMPY = DYNAMICS_ALIAS()

ArrayLike = Union[DYNAMICS_ALIAS.registered_types()]
Loading