Reduce required boilerplate for implementing external simulators and samplers
See original GitHub issueI wrote a custom simulator recently and I had to copy quite a bit of boilerplate code out of cirq/sim/sparse_simulator.py. Below is my code with the boilerplate pointed out. It would be nice if the code in SimulatesSamples and SimulatesIntermediateState parent classes were rearranged to reduce this. @Strilanc
class MySimulator(cirq.SimulatesSamples,
cirq.SimulatesIntermediateState):
# Boilerplate
def _run(self, circuit, param_resolver, repetitions):
param_resolver = param_resolver or cirq.ParamResolver({})
resolved_circuit = cirq.resolve_parameters(circuit, param_resolver)
assert not cirq.is_parameterized(resolved_circuit)
return self._run_sweep_repeat(resolved_circuit, repetitions)
# Boilerplate
def _run_sweep_repeat(self, circuit, repetitions):
measurements = defaultdict(list)
for _ in range(repetitions):
all_step_results = self._base_iterator(
circuit,
qubit_order=cirq.QubitOrder.DEFAULT,
initial_state=0)
for step_result in all_step_results:
for k, v in step_result.measurements.items():
measurements[k].append(np.array(v, dtype=np.uint8))
return {k: np.array(v, dtype=np.uint8) for k, v in measurements.items()}
# Boilerplate
def _simulator_iterator(self, circuit, param_resolver, qubit_order,
initial_state):
param_resolver = param_resolver or cirq.ParamResolver({})
resolved_circuit = cirq.resolve_parameters(circuit, param_resolver)
assert not cirq.is_parameterized(resolved_circuit)
actual_initial_state = 0 if initial_state is None else initial_state
return self._base_iterator(resolved_circuit, qubit_order,
actual_initial_state,
perform_measurements=True)
def _base_iterator(self, circuit, qubit_order, initial_state,
perform_measurements=True):
# Boilerplate
qubits = cirq.QubitOrder.as_qubit_order(qubit_order).order_for(
circuit.all_qubits())
num_qubits = len(qubits)
qid_shape = cirq.qid_shape(qubits)
qubit_map = {q: i for i, q in enumerate(qubits)}
if len(circuit) == 0:
yield MySimulatorStep(<default state>, {}, qubit_map)
def on_stuck(bad_op):
return TypeError(
"Can't simulate unknown operations that don't specify a"
"unitary or a decomposition. {!r}".format(bad_op))
def keep(potential_op):
return (cirq.has_unitary(potential_op) or
cirq.has_mixture(potential_op) or
cirq.is_measurement(potential_op) or
cirq.op_gate_isinstance(potential_op, cirq.ResetChannel))
simulator_state = ... # New code
for moment in circuit:
measurements = defaultdict(list)
known_ops = cirq.decompose(moment, keep=keep,
on_stuck_raise=on_stuck)
for op in known_ops:
self.simulate_op(op, simulator_state) # New code
yield MySimulatorStep(simulator_state, measurements, qubit_map)
def simulate_op(op, state):
# Actual simulation code here
...
class MySimulatorStep(cirq.StepResult):
# Boilerplate
def __init__(self, state, measurements, qubit_map):
super().__init__(measurements)
self.state = state
self.qubit_map = qubit_map
# Boilerplate
def _simulator_state(self):
return self.state
def sample(self, qubits, repetitions=1):
# Actual sampling code here
return ...
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (7 by maintainers)
Top Results From Across the Web
thecodingmachine/react-native-boilerplate - GitHub
This project is a React Native boilerplate that can be used to kickstart a mobile application. The boilerplate provides an optimized architecture for...
Read more >Metal Sample Code - Apple Developer
View sample code to see how Metal APIs are implemented. ... Reduce render workloads while increasing image detail with MetalFX. The sample shows...
Read more >Boilerplate – Office for Proposal Enhancement - UGA research
Through its programs and practices, it seeks to foster the understanding of and respect for cultural differences necessary for an enlightened and educated ......
Read more >Modeling and simulation for emergency response - GovInfo
A case was made for using simulation for emergency response. ... and believe most products are too boilerplate and hard to tailor to...
Read more >Apache Hadoop 3.3.4 – MapReduce Tutorial
Let us first take the Mapper and Reducer interfaces. Applications typically implement them to provide the map and reduce methods. We will then ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Is anyone working on this? It appears simulator has changed a bit since the issue was reported. Are there still improvements that can be made here? I may be interested if there is reasonable ROI. Presumably this is big enough to require a design first?
@tonybruguier everything should still be backward compatible. Overriding
_simulator_iterator
still works, is just marked deprecated in favor of overriding_base_iterator
. (Everyone’s_simulator_iterator
code was identical so we pulled it into the base class.)The only issue I found in any existing simulator was a single unit test that counted compiler warnings, which changed due to the new deprecation warning. All else should continue to work as it was.