Fix #49: reject an empty batch before it spawns a Python process - #98
Open
corgab wants to merge 2 commits into
Open
Fix #49: reject an empty batch before it spawns a Python process#98corgab wants to merge 2 commits into
corgab wants to merge 2 commits into
Conversation
BatchBuilder accepted an empty circuit list: both validation loops were no-ops and run() went on to start a full Python interpreter, import the Braket SDK and come back with no results, silently hiding what is almost always a programming mistake. The constructor now throws InvalidCircuitException::emptyBatch(), so Quantum::batch([]) fails at the call site, consistent with measure([]) refusing a measurement of nothing. Closes #49
Quantum::batch([]) now fails in the builder, but executeBatch([]) on a driver is public API through the BatchableDevice contract and still went through to Python; the driver refuses it before preflight as well.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
BatchBuilderaccepted an empty circuit list. The constructor's driver-mismatch loop andrun()'s per-circuit validation were both no-ops, soQuantum::batch([])->run()went straight toexecuteBatch([]), which started a full Python interpreter, imported the Braket SDK and came back with{"results": []}. An application that filters a circuit list down to nothing got an emptyBatchResultand no signal of the mistake, at the price of a subprocess spawn for no work. The same applied toexecuteBatch([])called directly on a driver.Plan
Of the two options in the issue, throw rather than return an empty result: the package already refuses operations that would do nothing (
measure([])throwsemptyMeasurementTargets()), and an empty batch is almost always a bug in the caller. Fail in the builder's constructor soQuantum::batch([])throws at the call site, and in the driver so theBatchableDevicepath is covered too.Changes
BatchBuilder::__construct(): throwsInvalidCircuitException::emptyBatch()for an empty list, before the driver-mismatch loop.AbstractQuantumDriver::executeBatch(): the same guard beforepreflight(), so a directexecuteBatch([])never reaches the bridge.InvalidCircuitException::emptyBatch(): says at least one circuit is needed and why an empty batch is refused.No config change, no new dependencies, no change for non-empty batches.
Tests
BatchBuilderTest: an empty batch throws with the expected message and the fake device records no batch run.AbstractQuantumDriverTest:executeBatch([])throws and the bridge'sexecute()is never called.main.Local: pint passed, pest 793 tests passed.
Closes #49