Skip to content

Commit e870363

Browse files
authored
add DecomposeCcxPass to run_qir_cpu (#3107)
This PR adds the `DecomposeCcxPass` to the `run_qir_cpu` function, which will enable correct handling of CCX gates through the `run_qir(..., type="cpu")` codepath.
1 parent 1b4e3db commit e870363

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

source/pip/qsharp/_simulation.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,7 @@ def run_qir_cpu(
514514
seed: Optional[int] = None,
515515
) -> List:
516516
(mod, shots, noise, seed) = preprocess_simulation_input(input, shots, noise, seed)
517+
DecomposeCcxPass().run(mod)
517518
if noise is None:
518519
(gates, num_qubits, num_results) = AggregateGatesPass().run(mod)
519520
else:

source/pip/tests/test_cpu_simulator.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,3 +474,22 @@ def test_cpu_permuted_rotations(noisy_gate: int, noise_number: int):
474474
assert (
475475
abs(actual_count - expected_count) <= tolerance
476476
), f"Count for {pattern} off by more than {tolerance_percent:.1f}% of shots. Actual={actual_count}, Expected={expected_count:.0f}, noise#{noise_number}, Program={program}."
477+
478+
479+
def test_ccx_gate_gets_decomposed():
480+
qsharp.init(target_profile=TargetProfile.Base)
481+
program = """
482+
operation Main() : Result[] {
483+
use qs = Qubit[3];
484+
X(qs[0]);
485+
X(qs[1]);
486+
CCNOT(qs[0], qs[1], qs[2]);
487+
MeasureEachZ(qs)
488+
}
489+
"""
490+
qsharp.eval(program)
491+
input = qsharp.compile("Main()")
492+
output = run_qir_cpu(str(input), shots=1000)
493+
result = [result_array_to_string(cast(Sequence[Result], x)) for x in output]
494+
histogram = Counter(result)
495+
assert histogram.get("111", 0) == 1000

0 commit comments

Comments
 (0)