Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ $result->outcomes(); // ['00', '11'] — bitstrings sorted by count, desc
```php
$builder = Quantum::circuit()->qubits(2)->h(0)->cnot(0, 1)->measure();

$builder->gateCount(); // 2 — number of gates, excluding measurement
$builder->depth(); // 2 — number of sequential layers, excluding measurement
$builder->gateCount(); // 2 — number of gates, excluding measurement
$builder->depth(); // 2 — number of sequential layers, excluding measurement
$builder->hasMeasurement(); // true — whether any gate is a measurement
```

### Entropy Generation
Expand Down
31 changes: 21 additions & 10 deletions src/Circuit/CircuitBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ class CircuitBuilder
/** @var Gate[] */
private array $gates = [];

private bool $hasMeasurement = false;

private int $shots = 1000;

final public function __construct(
Expand Down Expand Up @@ -619,17 +617,34 @@ public function validate(): static
throw InvalidCircuitException::noQubits();
}

if (! $this->hasMeasurement) {
if (! $this->hasMeasurement()) {
throw InvalidCircuitException::noMeasurement();
}

return $this;
}

/**
* Validate a gate against the circuit's qubit range, append it, and
* track measurement state. The single append primitive behind every
* fluent gate method and fromArray().
* Whether any gate in the circuit is a measurement.
*
* Derived from $gates on demand, like gateCount() and depth(), rather
* than tracked in a flag push() would have to keep in step; circuits are
* tens of gates at most, so the scan costs nothing measurable.
*/
public function hasMeasurement(): bool
{
foreach ($this->gates as $gate) {
if ($gate->isMeasurement()) {
return true;
}
}

return false;
}

/**
* Validate a gate against the circuit's qubit range and append it. The
* single append primitive behind every fluent gate method and fromArray().
*
* @throws InvalidCircuitException
*/
Expand All @@ -639,10 +654,6 @@ private function push(Gate $gate): static

$this->gates[] = $gate;

if ($gate->isMeasurement()) {
$this->hasMeasurement = true;
}

return $this;
}

Expand Down
19 changes: 19 additions & 0 deletions tests/Unit/Circuit/CircuitBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,25 @@
// run() — validation: no measurement
// -------------------------------------------------------------------------

it('reports whether the circuit measures anything, derived from its gates', function () use (&$builder): void {
$builder->qubits(2)->h(0)->cnot(0, 1);

expect($builder->hasMeasurement())->toBeFalse();

$builder->measure(1);

expect($builder->hasMeasurement())->toBeTrue();
});

it('does not count a measurement dropped from an appended fragment', function () use (&$device, &$builder): void {
$fragment = (new CircuitBuilder($device))->qubits(1)->x(0)->measure();

$builder->qubits(1)->append($fragment);

expect($builder->hasMeasurement())->toBeFalse()
->and(fn () => $builder->run())->toThrow(InvalidCircuitException::class);
});

it('run throws when no measurement', function () use (&$builder): void {
expect(fn () => $builder->qubits(1)->h(0)->run())
->toThrow(InvalidCircuitException::class);
Expand Down