@@ -377,6 +377,33 @@ def test_simulator_set_wavefunction(sim):
377377 Measure | qubits
378378
379379
380+ def test_simulator_collapse_wavefunction (sim ):
381+ eng = MainEngine (sim )
382+ qubits = eng .allocate_qureg (4 )
383+ # unknown qubits: raises
384+ with pytest .raises (RuntimeError ):
385+ eng .backend .collapse_wavefunction (qubits , [0 ] * 4 )
386+ eng .flush ()
387+ eng .backend .collapse_wavefunction (qubits , [0 ] * 4 )
388+ assert pytest .approx (eng .backend .get_probability ([0 ] * 4 , qubits )) == 1.
389+ All (H ) | qubits [1 :]
390+ eng .flush ()
391+ assert pytest .approx (eng .backend .get_probability ([0 ] * 4 , qubits )) == .125
392+ # impossible outcome: raises
393+ with pytest .raises (RuntimeError ):
394+ eng .backend .collapse_wavefunction (qubits , [1 ] + [0 ] * 3 )
395+ eng .backend .collapse_wavefunction (qubits [:- 1 ], [0 , 1 , 0 ])
396+ assert (pytest .approx (eng .backend .get_probability ([0 , 1 , 0 , 1 ], qubits ))
397+ == .5 )
398+ eng .backend .set_wavefunction ([1. ] + [0. ] * 15 , qubits )
399+ H | qubits [0 ]
400+ CNOT | (qubits [0 ], qubits [1 ])
401+ eng .flush ()
402+ eng .backend .collapse_wavefunction ([qubits [0 ]], [1 ])
403+ assert (pytest .approx (eng .backend .get_probability ([1 , 1 ], qubits [0 :2 ]))
404+ == 1. )
405+
406+
380407def test_simulator_no_uncompute_exception (sim ):
381408 eng = MainEngine (sim , [])
382409 qubit = eng .allocate_qubit ()
0 commit comments