Skip to content

Commit

Permalink
Merge branch 'fix/sympy_eval' into issues/192_sympy_tests
Browse files Browse the repository at this point in the history
  • Loading branch information
terrorfisch committed Mar 15, 2018
2 parents 9cd4c68 + 09f8128 commit 95965b7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
4 changes: 2 additions & 2 deletions qctoolkit/pulses/loop_pulse_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,8 @@ def deserialize(serializer: Serializer,
loop_range: Tuple,
loop_index: str,
identifier: Optional[str]=None,
measurements: Optional=None,
parameter_constraints: Optional=None) -> 'ForLoopPulseTemplate':
measurements: Optional[Sequence[str]]=None,
parameter_constraints: Optional[Sequence[str]]=None) -> 'ForLoopPulseTemplate':
body = cast(PulseTemplate, serializer.deserialize(body))
return ForLoopPulseTemplate(body=body,
identifier=identifier,
Expand Down
10 changes: 9 additions & 1 deletion qctoolkit/pulses/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from typing import Dict, Tuple, Any, Generator, Optional

import numpy as np
import warnings

from qctoolkit.utils.types import ChannelID
from qctoolkit.pulses.pulse_template import PulseTemplate
Expand Down Expand Up @@ -44,9 +45,14 @@ def iter_waveforms(instruction_block: AbstractInstructionBlock,
raise NotImplementedError('Rendering cannot handle instructions of type {}.'.format(type(instruction)))


def render(sequence: AbstractInstructionBlock, sample_rate: int=10) -> Tuple[np.ndarray, Dict[ChannelID, np.ndarray]]:
def render(sequence: AbstractInstructionBlock, sample_rate: float=10.0) -> Tuple[np.ndarray, Dict[ChannelID, np.ndarray]]:
"""'Render' an instruction sequence (sample all contained waveforms into an array).
Args:
sequence (AbstractInstructionBlock): block of instructions representing a (sub)sequence
in the control flow of a pulse template instantiation.
sample_rate (float): The sample rate in GHz.
Returns:
a tuple (times, values) of numpy.ndarrays of similar size. times contains the time value
of all sample times and values the corresponding sampled value.
Expand All @@ -61,6 +67,8 @@ def render(sequence: AbstractInstructionBlock, sample_rate: int=10) -> Tuple[np.

# add one sample to see the end of the waveform
sample_count = total_time * sample_rate + 1
if not float(sample_count).is_integer():
warnings.warn('Sample count not whole number. Casted to integer.')
times = np.linspace(0, total_time, num=sample_count, dtype=float)
# move the last sample inside the waveform
times[-1] = np.nextafter(times[-1], times[-2])
Expand Down
5 changes: 2 additions & 3 deletions qctoolkit/utils/sympy.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,8 @@ def sympify(expr: Union[str, Number, sympy.Expr, numpy.str_], **kwargs) -> sympy
def substitute_with_eval(expression: sympy.Expr,
substitutions: Dict[str, Union[sympy.Expr, numpy.ndarray, str]]) -> sympy.Expr:
"""Substitutes only sympy.Symbols. Workaround for numpy like array behaviour. ~Factor 3 slower compared to subs"""
for k, v in substitutions.items():
if not isinstance(v, sympy.Expr):
substitutions[k] = sympify(v)
substitutions = {k: v if isinstance(v, sympy.Expr) else sympify(v)
for k, v in substitutions.items()}

for symbol in expression.free_symbols:
symbol_name = str(symbol)
Expand Down

0 comments on commit 95965b7

Please sign in to comment.