Skip to content

Commit

Permalink
Update for v1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
WrathfulSpatula committed May 16, 2023
1 parent 7c95a48 commit e948eb9
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 14 deletions.
56 changes: 43 additions & 13 deletions qrack/qasm_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
import numpy as np
import scipy as sp
import collections
from typing import Dict
from typing import (
Dict,
Iterator
)
from pyqrack import QrackSimulator, Pauli

import cirq
Expand Down Expand Up @@ -104,6 +107,40 @@ def __init__(self,
self._shots = {}
self._local_random = np.random.RandomState()

def run_sweep_iter(
self, program: 'cirq.AbstractCircuit', params: 'cirq.Sweepable', repetitions: int = 1
) -> Iterator['cirq.Result']:
"""Runs the supplied Circuit, mimicking quantum hardware.
In contrast to run, this allows for sweeping over different parameter
values.
Args:
program: The circuit to simulate.
params: Parameters to run with the program.
repetitions: The number of repetitions to simulate.
Returns:
Result list for this run; one for each possible parameter
resolver.
Raises:
ValueError: If the circuit has no measurements.
"""
if not program.has_measurements():
raise ValueError("Circuit has no measurements to sample.")

for param_resolver in study.to_resolvers(params):
records = {}
if repetitions == 0:
for _, op, _ in program.findall_operations_with_gate_type(ops.MeasurementGate):
records[protocols.measurement_key_name(op)] = np.empty([0, 1, 1])
else:
measurements = self._run(
circuit=program, param_resolver=param_resolver, repetitions=repetitions
)
yield study.ResultDict(params=param_resolver, measurements=measurements)

def _run(
self, circuit: circuits.Circuit, param_resolver: study.ParamResolver, repetitions: int
) -> Dict[str, np.ndarray]:
Expand Down Expand Up @@ -158,12 +195,7 @@ def _run(
for _ in op.qubits:
value.append(sample[qb_index])
qb_index = qb_index + 1
self._memory[key].append(np.asarray([value]))

__memory = {}
for key, value in self._memory.items():
__memory[key] = np.asarray(value)
self._memory = __memory
self._memory[key] += [value]

return self._memory

Expand All @@ -177,16 +209,14 @@ def _run(
for op in operations:
indices = [num_qubits - 1 - qubit_map[qubit] for qubit in op.qubits]
key = protocols.measurement_key_name(op.gate)
self._memory[key].append(np.asarray([self._add_qasm_measure(indices)]))

__memory = {}
for key, value in self._memory.items():
__memory[key] = np.asarray(value)
self._memory = __memory
self._memory[key] = [self._add_qasm_measure(indices)]

return self._memory

def _try_gate(self, op: ops.GateOperation, indices: np.array):
if isinstance(op.gate, ops.IdentityGate):
return True

# One qubit gate
if isinstance(op.gate, ops.pauli_gates._PauliX):
self._sim.x(indices[0])
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
]

# Handle version.
VERSION = "0.5.1"
VERSION = "0.6.0"

# Read long description from README.
README_PATH = os.path.join(os.path.abspath(os.path.dirname(__file__)),
Expand Down

0 comments on commit e948eb9

Please sign in to comment.