Skip to content

Commit

Permalink
Merge pull request #69 from Blueqat/ignore_global
Browse files Browse the repository at this point in the history
Add option for ignore global phase
  • Loading branch information
gyu-don authored Sep 19, 2019
2 parents 9c78ae5 + bb4d47d commit b9deb84
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
14 changes: 8 additions & 6 deletions blueqat/backends/_numba_backend_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,9 @@ def _reduce1(qubits, target, n_qubits, p0):
class NumbaBackend(Backend):
"""Simulator backend which uses numba."""
__return_type = {
"statevector": lambda ctx: _ignore_globals(ctx.qubits),
"statevector": lambda ctx: ctx.qubits,
"shots": lambda ctx: ctx.shots_result,
"statevector_and_shots": lambda ctx: (_ignore_globals(ctx.qubits), ctx.shots_result),
"statevector_and_shots": lambda ctx: (ctx.qubits, ctx.shots_result),
"_inner_ctx": lambda ctx: ctx,
}
DEFAULT_SHOTS = 1024
Expand All @@ -353,7 +353,7 @@ def __clear_cache_if_invalid(self, n_qubits, dtype):
return

def run(self, gates, n_qubits, *args, **kwargs):
def __parse_run_args(shots=None, returns=None, enable_cache=True,
def __parse_run_args(shots=None, returns=None, enable_cache=True, ignore_global=True,
dtype=DEFAULT_DTYPE, **_kwargs):
if returns is None:
if shots is None:
Expand All @@ -369,9 +369,9 @@ def __parse_run_args(shots=None, returns=None, enable_cache=True,
shots = self.DEFAULT_SHOTS
if returns == "statevector" and shots > 1:
warnings.warn("When `returns` = 'statevector', `shots` = 1 is enough.")
return shots, returns, dtype, enable_cache
return shots, returns, dtype, enable_cache, ignore_global

shots, returns, dtype, enable_cache = __parse_run_args(*args, **kwargs)
shots, returns, dtype, enable_cache, ignore_global = __parse_run_args(*args, **kwargs)

if enable_cache:
self.__clear_cache_if_invalid(n_qubits, dtype)
Expand Down Expand Up @@ -402,6 +402,8 @@ def run_single_gate(gate):
if ctx.cregs:
ctx.store_shot()

if ignore_global:
_ignore_global(ctx.qubits)
return self.__return_type[returns](ctx)

def make_cache(self, gates, n_qubits):
Expand Down Expand Up @@ -602,7 +604,7 @@ def gate_measure(self, gate, ctx):


@njit(nogil=True, cache=True)
def _ignore_globals(qubits):
def _ignore_global(qubits):
for q in qubits:
if abs(q) > 0.0000001:
ang = abs(q) / q
Expand Down
16 changes: 7 additions & 9 deletions blueqat/backends/numpy_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ def to_str(cregs):
class NumPyBackend(Backend):
"""Simulator backend which uses numpy. This backend is Blueqat's default backend."""
__return_type = {
"statevector": lambda ctx: _ignore_globals(ctx.qubits),
"statevector": lambda ctx: ctx.qubits,
"shots": lambda ctx: ctx.shots_result,
"statevector_and_shots": lambda ctx: (_ignore_globals(ctx.qubits), ctx.shots_result),
"statevector_and_shots": lambda ctx: (ctx.qubits, ctx.shots_result),
"_inner_ctx": lambda ctx: ctx,
}
DEFAULT_SHOTS = 1024
Expand All @@ -85,7 +85,7 @@ def __clear_cache_if_invalid(self, n_qubits, dtype):
return

def run(self, gates, n_qubits, *args, **kwargs):
def __parse_run_args(shots=None, returns=None, **_kwargs):
def __parse_run_args(shots=None, returns=None, ignore_global=True, **_kwargs):
if returns is None:
if shots is None:
returns = "statevector"
Expand All @@ -100,9 +100,9 @@ def __parse_run_args(shots=None, returns=None, **_kwargs):
shots = self.DEFAULT_SHOTS
if returns == "statevector" and shots > 1:
warnings.warn("When `returns` = 'statevector', `shots` = 1 is enough.")
return shots, returns
return shots, returns, ignore_global

shots, returns = __parse_run_args(*args, **kwargs)
shots, returns, ignore_global = __parse_run_args(*args, **kwargs)

self.__clear_cache_if_invalid(n_qubits, DEFAULT_DTYPE)
ctx = _NumPyBackendContext(n_qubits)
Expand All @@ -126,6 +126,8 @@ def run_single_gate(gate):
if ctx.cregs:
ctx.store_shot()

if ignore_global:
ignore_global_phase(ctx.qubits)
return self.__return_type[returns](ctx)

def make_cache(self, gates, n_qubits):
Expand Down Expand Up @@ -386,7 +388,3 @@ def gate_measure(self, gate, ctx):
ctx.cregs[target] = 1
ctx.save_cache = False
return ctx


def _ignore_globals(qubits):
return ignore_global_phase(qubits)

0 comments on commit b9deb84

Please sign in to comment.