def test_command_comparison(main_engine): qubit = Qureg([Qubit(main_engine, 0)]) ctrl_qubit = Qureg([Qubit(main_engine, 1)]) cmd1 = _command.Command(main_engine, Rx(0.5), (qubit,)) cmd1.tags = ["TestTag"] cmd1.add_control_qubits(ctrl_qubit) # Test equality cmd2 = _command.Command(main_engine, Rx(0.5), (qubit,)) cmd2.tags = ["TestTag"] cmd2.add_control_qubits(ctrl_qubit) assert cmd2 == cmd1 # Test not equal because of tags cmd3 = _command.Command(main_engine, Rx(0.5), (qubit,)) cmd3.tags = ["TestTag", "AdditionalTag"] cmd3.add_control_qubits(ctrl_qubit) assert not cmd3 == cmd1 # Test not equal because of control qubit cmd4 = _command.Command(main_engine, Rx(0.5), (qubit,)) cmd4.tags = ["TestTag"] assert not cmd4 == cmd1 # Test not equal because of qubit qubit2 = Qureg([Qubit(main_engine, 2)]) cmd5 = _command.Command(main_engine, Rx(0.5), (qubit2,)) cmd5.tags = ["TestTag"] cmd5.add_control_qubits(ctrl_qubit) assert cmd5 != cmd1 # Test not equal because of engine cmd6 = _command.Command("FakeEngine", Rx(0.5), (qubit,)) cmd6.tags = ["TestTag"] cmd6.add_control_qubits(ctrl_qubit) assert cmd6 != cmd1
def test_tensor_or(): saving_backend = DummyEngine(save_commands=True) main_engine = MainEngine(backend=saving_backend, engine_list=[DummyEngine()]) gate = Rx(0.6) qubit0 = Qubit(main_engine, 0) qubit1 = Qubit(main_engine, 1) qubit2 = Qubit(main_engine, 2) # Option 1: _metagates.Tensor(gate) | ([qubit0, qubit1, qubit2], ) # Option 2: _metagates.Tensor(gate) | [qubit0, qubit1, qubit2] received_commands = [] # Remove Allocate and Deallocate gates for cmd in saving_backend.received_commands: if not (isinstance(cmd.gate, FastForwardingGate) or isinstance(cmd.gate, ClassicalInstructionGate)): received_commands.append(cmd) # Check results assert len(received_commands) == 6 qubit_ids = [] for cmd in received_commands: assert len(cmd.qubits) == 1 assert cmd.gate == gate qubit_ids.append(cmd.qubits[0][0].id) assert sorted(qubit_ids) == [0, 0, 1, 1, 2, 2]
def test_controlled_gate_or(): saving_backend = DummyEngine(save_commands=True) main_engine = MainEngine(backend=saving_backend, engine_list=[DummyEngine()]) gate = Rx(0.6) qubit0 = Qubit(main_engine, 0) qubit1 = Qubit(main_engine, 1) qubit2 = Qubit(main_engine, 2) qubit3 = Qubit(main_engine, 3) expected_cmd = Command(main_engine, gate, ([qubit3], )) expected_cmd.add_control_qubits([qubit0, qubit1, qubit2]) received_commands = [] # Option 1: _metagates.ControlledGate(gate, 3) | ([qubit1], [qubit0], [qubit2 ], [qubit3]) # Option 2: _metagates.ControlledGate(gate, 3) | (qubit1, qubit0, qubit2, qubit3) # Option 3: _metagates.ControlledGate(gate, 3) | ([qubit1, qubit0], qubit2, qubit3) # Option 4: _metagates.ControlledGate(gate, 3) | (qubit1, [qubit0, qubit2], qubit3) # Wrong option 5: with pytest.raises(_metagates.ControlQubitError): _metagates.ControlledGate(gate, 3) | (qubit1, [qubit0, qubit2, qubit3]) # Remove Allocate and Deallocate gates for cmd in saving_backend.received_commands: if not (isinstance(cmd.gate, FastForwardingGate) or isinstance(cmd.gate, ClassicalInstructionGate)): received_commands.append(cmd) assert len(received_commands) == 4 for cmd in received_commands: assert cmd == expected_cmd
def test_command_init(main_engine): qureg0 = Qureg([Qubit(main_engine, 0)]) qureg1 = Qureg([Qubit(main_engine, 1)]) qureg2 = Qureg([Qubit(main_engine, 2)]) # qureg3 = Qureg([Qubit(main_engine, 3)]) # qureg4 = Qureg([Qubit(main_engine, 4)]) gate = BasicGate() cmd = _command.Command(main_engine, gate, (qureg0, qureg1, qureg2)) assert cmd.gate == gate assert cmd.tags == [] expected_tuple = (qureg0, qureg1, qureg2) for cmd_qureg, expected_qureg in zip(cmd.qubits, expected_tuple): assert cmd_qureg[0].id == expected_qureg[0].id # Testing that Qubits are now WeakQubitRef objects assert type(cmd_qureg[0]) == WeakQubitRef assert cmd._engine == main_engine # Test that quregs are ordered if gate has interchangeable qubits: symmetric_gate = BasicGate() symmetric_gate.interchangeable_qubit_indices = [[0, 1]] symmetric_cmd = _command.Command(main_engine, symmetric_gate, (qureg2, qureg1, qureg0)) assert cmd.gate == gate assert cmd.tags == [] expected_ordered_tuple = (qureg1, qureg2, qureg0) for cmd_qureg, expected_qureg in zip(symmetric_cmd.qubits, expected_ordered_tuple): assert cmd_qureg[0].id == expected_qureg[0].id assert symmetric_cmd._engine == main_engine
def test_basic_gate_or(): saving_backend = DummyEngine(save_commands=True) main_engine = MainEngine(backend=saving_backend, engine_list=[DummyEngine()]) qubit0 = Qubit(main_engine, 0) qubit1 = Qubit(main_engine, 1) qubit2 = Qubit(main_engine, 2) qubit3 = Qubit(main_engine, 3) qureg = Qureg([qubit2, qubit3]) basic_gate = _basics.BasicGate() command1 = basic_gate.generate_command(qubit0) basic_gate | qubit0 command2 = basic_gate.generate_command([qubit0, qubit1]) basic_gate | [qubit0, qubit1] command3 = basic_gate.generate_command(qureg) basic_gate | qureg command4 = basic_gate.generate_command((qubit0,)) basic_gate | (qubit0,) command5 = basic_gate.generate_command((qureg, qubit0)) basic_gate | (qureg, qubit0) received_commands = [] # Remove Deallocate gates for cmd in saving_backend.received_commands: if not isinstance(cmd.gate, _basics.FastForwardingGate): received_commands.append(cmd) assert received_commands == ([command1, command2, command3, command4, command5])
def test_commmand_add_control_qubits(main_engine): qubit0 = Qureg([Qubit(main_engine, 0)]) qubit1 = Qureg([Qubit(main_engine, 1)]) qubit2 = Qureg([Qubit(main_engine, 2)]) cmd = _command.Command(main_engine, Rx(0.5), (qubit0, )) cmd.add_control_qubits(qubit2 + qubit1) assert cmd.control_qubits[0].id == 1 assert cmd.control_qubits[1].id == 2
def test_command_all_qubits(main_engine): qubit0 = Qureg([Qubit(main_engine, 0)]) qubit1 = Qureg([Qubit(main_engine, 1)]) cmd = _command.Command(main_engine, Rx(0.5), (qubit0,)) cmd.add_control_qubits(qubit1) all_qubits = cmd.all_qubits assert all_qubits[0][0].id == 1 assert all_qubits[1][0].id == 0
def test_commmand_add_control_qubits_two(main_engine, state): qubit0 = Qureg([Qubit(main_engine, 0)]) qubit1 = Qureg([Qubit(main_engine, 1)]) qubit2 = Qureg([Qubit(main_engine, 2)]) qubit3 = Qureg([Qubit(main_engine, 3)]) cmd = _command.Command(main_engine, Rx(0.5), (qubit0,), qubit1) cmd.add_control_qubits(qubit2 + qubit3, state) assert cmd.control_qubits[0].id == 1 assert cmd.control_state == '1' + canonical_ctrl_state(state, 2)
def test_command_str(): qubit = Qureg([Qubit(main_engine, 0)]) ctrl_qubit = Qureg([Qubit(main_engine, 1)]) cmd = _command.Command(main_engine, Rx(0.5), (qubit, )) cmd.tags = ["TestTag"] cmd.add_control_qubits(ctrl_qubit) assert str(cmd) == "CRx(0.5) | ( Qureg[1], Qureg[0] )" cmd2 = _command.Command(main_engine, Rx(0.5), (qubit, )) assert str(cmd2) == "Rx(0.5) | Qureg[0]"
def test_command_engine(main_engine): qubit0 = Qureg([Qubit("fake_engine", 0)]) qubit1 = Qureg([Qubit("fake_engine", 1)]) cmd = _command.Command("fake_engine", Rx(0.5), (qubit0, )) cmd.add_control_qubits(qubit1) assert cmd.engine == "fake_engine" cmd.engine = main_engine assert id(cmd.engine) == id(main_engine) assert id(cmd.control_qubits[0].engine) == id(main_engine) assert id(cmd.qubits[0][0].engine) == id(main_engine)
def test_commmand_add_control_qubits_one(main_engine, state): qubit0 = Qureg([Qubit(main_engine, 0)]) qubit1 = Qureg([Qubit(main_engine, 1)]) cmd = _command.Command(main_engine, Rx(0.5), (qubit0,)) cmd.add_control_qubits(qubit1, state=state) assert cmd.control_qubits[0].id == 1 assert cmd.control_state == canonical_ctrl_state(state, 1) with pytest.raises(ValueError): cmd.add_control_qubits(qubit0[0])
def test_tensored_controlled_gate(): saving_backend = DummyEngine(save_commands=True) main_engine = MainEngine(backend=saving_backend, engine_list=[DummyEngine()]) gate = Rx(0.6) qubit0 = Qubit(main_engine, 0) qubit1 = Qubit(main_engine, 1) qubit2 = Qubit(main_engine, 2) target_qubits = [qubit1, qubit2] C(All(gate)) | (qubit0, target_qubits) assert saving_backend.received_commands[-1].gate == gate assert len(saving_backend.received_commands[-1].control_qubits) == 1
def test_command_str(main_engine): qubit = Qureg([Qubit(main_engine, 0)]) ctrl_qubit = Qureg([Qubit(main_engine, 1)]) cmd = _command.Command(main_engine, Rx(0.5 * math.pi), (qubit,)) cmd.tags = ["TestTag"] cmd.add_control_qubits(ctrl_qubit) cmd2 = _command.Command(main_engine, Rx(0.5 * math.pi), (qubit,)) if sys.version_info.major == 3: assert cmd.to_string(symbols=False) == "CRx(1.570796326795) | ( Qureg[1], Qureg[0] )" assert str(cmd2) == "Rx(1.570796326795) | Qureg[0]" else: assert cmd.to_string(symbols=False) == "CRx(1.5707963268) | ( Qureg[1], Qureg[0] )" assert str(cmd2) == "Rx(1.5707963268) | Qureg[0]"
def test_command_engine(main_engine): qubit0 = Qureg([Qubit("fake_engine", 0)]) qubit1 = Qureg([Qubit("fake_engine", 1)]) cmd = _command.Command("fake_engine", Rx(0.5), (qubit0,)) cmd.add_control_qubits(qubit1) assert cmd.engine == "fake_engine" cmd.engine = main_engine assert id(cmd.engine) == id(main_engine) assert id(cmd.control_qubits[0].engine) == id(main_engine) assert id(cmd.qubits[0][0].engine) == id(main_engine) # Avoid raising exception upon Qubit destructions qubit0[0].id = -1 qubit1[0].id = -1
def allocate_qureg(self, n, init=0.0): """ Allocate n qubits and return them as a quantum register, which is a list of qubit objects. Args: n (int): Number of qubits to allocate init (complex): Assign this value to every amplitude Returns: Qureg of length n, a list of n newly allocated qubits. """ cmd = Command(self, AllocateQuregGate(init), ()) if self.backend.is_available(cmd): qubits = Qureg() for i in range(n): new_id = self.main_engine.get_new_qubit_id() qb = Qubit(self, new_id) qubits.append(qb) self.main_engine.active_qubits.add(qb) cmd = Command(self, AllocateQuregGate(init), (qubits, )) self.send([cmd]) return qubits else: return super(MainEngine, self).allocate_qureg(n)
def test_basic_gate_make_tuple_of_qureg(main_engine): qubit0 = Qubit(main_engine, 0) qubit1 = Qubit(main_engine, 1) qubit2 = Qubit(main_engine, 2) qubit3 = Qubit(main_engine, 3) qureg = Qureg([qubit2, qubit3]) case1 = _basics.BasicGate.make_tuple_of_qureg(qubit0) assert case1 == ([qubit0],) case2 = _basics.BasicGate.make_tuple_of_qureg([qubit0, qubit1]) assert case2 == ([qubit0, qubit1],) case3 = _basics.BasicGate.make_tuple_of_qureg(qureg) assert case3 == (qureg,) case4 = _basics.BasicGate.make_tuple_of_qureg((qubit0,)) assert case4 == ([qubit0],) case5 = _basics.BasicGate.make_tuple_of_qureg((qureg, qubit0)) assert case5 == (qureg, [qubit0])
def allocate_qubit(self, dirty=False): """ Return a new qubit as a list containing 1 qubit object (quantum register of size 1). Allocates a new qubit by getting a (new) qubit id from the MainEngine, creating the qubit object, and then sending an AllocateQubit command down the pipeline. If dirty=True, the fresh qubit can be replaced by a pre-allocated one (in an unknown, dirty, initial state). Dirty qubits must be returned to their initial states before they are deallocated / freed. All allocated qubits are added to the MainEngine's set of active qubits as weak references. This allows proper clean-up at the end of the Python program (using atexit), deallocating all qubits which are still alive. Qubit ids of dirty qubits are registered in MainEngine's dirty_qubits set. Args: dirty (bool): If True, indicates that the allocated qubit may be dirty (i.e., in an arbitrary initial state). Returns: Qureg of length 1, where the first entry is the allocated qubit. """ new_id = self.main_engine.get_new_qubit_id() qb = Qureg([Qubit(self, new_id)]) cmd = Command(self, Allocate, (qb, )) if dirty: from projectq.meta import ( # pylint: disable=import-outside-toplevel DirtyQubitTag, ) if self.is_meta_tag_supported(DirtyQubitTag): cmd.tags += [DirtyQubitTag()] self.main_engine.dirty_qubits.add(qb[0].id) self.main_engine.active_qubits.add(qb[0]) self.send([cmd]) return qb
def test_aqt_retrieve(monkeypatch): # patch send def mock_retrieve(*args, **kwargs): return [0, 6, 0, 6, 0, 0, 0, 6, 0, 6] monkeypatch.setattr(_aqt, "retrieve", mock_retrieve) backend = _aqt.AQTBackend( retrieve_execution="a3877d18-314f-46c9-86e7-316bc4dbe968", verbose=True) eng = MainEngine(backend=backend, engine_list=[]) unused_qubit = eng.allocate_qubit() qureg = eng.allocate_qureg(2) # entangle the qureg Ry(math.pi / 2) | qureg[0] Rx(math.pi / 2) | qureg[0] Rx(math.pi / 2) | qureg[0] Ry(math.pi / 2) | qureg[0] Rxx(math.pi / 2) | (qureg[0], qureg[1]) Rx(7 * math.pi / 2) | qureg[0] Ry(7 * math.pi / 2) | qureg[0] Rx(7 * math.pi / 2) | qureg[1] del unused_qubit # measure; should be all-0 or all-1 All(Measure) | qureg # run the circuit eng.flush() prob_dict = eng.backend.get_probabilities([qureg[0], qureg[1]]) assert prob_dict['11'] == pytest.approx(0.4) assert prob_dict['00'] == pytest.approx(0.6) # Unknown qubit and no mapper invalid_qubit = [Qubit(eng, 10)] with pytest.raises(RuntimeError): eng.backend.get_probabilities(invalid_qubit)
def test_basic_gate_generate_command(main_engine): qubit0 = Qubit(main_engine, 0) qubit1 = Qubit(main_engine, 1) qubit2 = Qubit(main_engine, 2) qubit3 = Qubit(main_engine, 3) qureg = Qureg([qubit2, qubit3]) basic_gate = _basics.BasicGate() command1 = basic_gate.generate_command(qubit0) assert command1 == Command(main_engine, basic_gate, ([qubit0], )) command2 = basic_gate.generate_command([qubit0, qubit1]) assert command2 == Command(main_engine, basic_gate, ([qubit0, qubit1], )) command3 = basic_gate.generate_command(qureg) assert command3 == Command(main_engine, basic_gate, (qureg, )) command4 = basic_gate.generate_command((qubit0, )) assert command4 == Command(main_engine, basic_gate, ([qubit0], )) command5 = basic_gate.generate_command((qureg, qubit0)) assert command5 == Command(main_engine, basic_gate, (qureg, [qubit0]))
def test_limited_capability_engine_classical_instructions(): default_eng = LimitedCapabilityEngine() eng = LimitedCapabilityEngine(allow_classical_instructions=False, allow_classes=[FlushGate]) m = MainEngine(backend=DummyEngine(), engine_list=[eng]) with pytest.raises(ValueError): _ = m.allocate_qubit() q = Qubit(m, 0) assert default_eng.is_available(Measure.generate_command(q)) assert not eng.is_available(Measure.generate_command(q))
def test_command_deepcopy(main_engine): qureg0 = Qureg([Qubit(main_engine, 0)]) qureg1 = Qureg([Qubit(main_engine, 1)]) gate = BasicGate() cmd = _command.Command(main_engine, gate, (qureg0,)) cmd.add_control_qubits(qureg1) cmd.tags.append("MyTestTag") copied_cmd = deepcopy(cmd) # Test that deepcopy gives same cmd assert copied_cmd.gate == gate assert copied_cmd.tags == ["MyTestTag"] assert len(copied_cmd.qubits) == 1 assert copied_cmd.qubits[0][0].id == qureg0[0].id assert len(copied_cmd.control_qubits) == 1 assert copied_cmd.control_qubits[0].id == qureg1[0].id # Engine should not be deepcopied but a reference: assert id(copied_cmd.engine) == id(main_engine) # Test that deepcopy is actually a deepcopy cmd.tags = ["ChangedTag"] assert copied_cmd.tags == ["MyTestTag"] cmd.control_qubits[0].id == 10 assert copied_cmd.control_qubits[0].id == qureg1[0].id cmd.gate = "ChangedGate" assert copied_cmd.gate == gate
def test_command_interchangeable_qubit_indices(main_engine): gate = BasicGate() gate.interchangeable_qubit_indices = [[0, 4, 5], [1, 2]] qubit0 = Qureg([Qubit(main_engine, 0)]) qubit1 = Qureg([Qubit(main_engine, 1)]) qubit2 = Qureg([Qubit(main_engine, 2)]) qubit3 = Qureg([Qubit(main_engine, 3)]) qubit4 = Qureg([Qubit(main_engine, 4)]) qubit5 = Qureg([Qubit(main_engine, 5)]) input_tuple = (qubit4, qubit5, qubit3, qubit2, qubit1, qubit0) cmd = _command.Command(main_engine, gate, input_tuple) assert (cmd.interchangeable_qubit_indices == [[0, 4, 5], [1, 2]] or cmd.interchangeable_qubit_indices == [[1, 2], [0, 4, 5]])
def test_command_order_qubits(main_engine): qubit0 = Qureg([Qubit(main_engine, 0)]) qubit1 = Qureg([Qubit(main_engine, 1)]) qubit2 = Qureg([Qubit(main_engine, 2)]) qubit3 = Qureg([Qubit(main_engine, 3)]) qubit4 = Qureg([Qubit(main_engine, 4)]) qubit5 = Qureg([Qubit(main_engine, 5)]) gate = BasicGate() gate.interchangeable_qubit_indices = [[0, 4, 5], [1, 2]] input_tuple = (qubit4, qubit5, qubit3, qubit2, qubit1, qubit0) expected_tuple = (qubit0, qubit3, qubit5, qubit2, qubit1, qubit4) cmd = _command.Command(main_engine, gate, input_tuple) for ordered_qubit, expected_qubit in zip(cmd.qubits, expected_tuple): assert ordered_qubit[0].id == expected_qubit[0].id
def test_addition_circuit(): commands = [] eng = None qs = [Qureg([Qubit(eng, idx=i)]) for i in range(10)] for i in reversed(range(10)): if i != 4: commands.append(CommandEx(eng, X, (qs[i], ), controls=qs[4])) for i in range(4): commands.append(CommandEx(eng, X, (qs[5 + i], ), controls=qs[4])) commands.append( CommandEx(eng, Swap, (qs[4], qs[i]), controls=qs[5 + i])) commands.append(CommandEx(eng, X, (qs[-1], ), controls=qs[4])) for i in range(4)[::-1]: commands.append( CommandEx(eng, Swap, (qs[4], qs[i]), controls=qs[5 + i])) commands.append(CommandEx(eng, X, (qs[5 + i], ), controls=qs[i])) for i in range(10): if i != 4: commands.append(CommandEx(eng, X, (qs[i], ), controls=qs[4])) assert commands_to_ascii_circuit(commands) == ''' |0⟩─────────────────⊕───×───────────────────────────×─•─⊕───────────────── │ │ │ │ │ |0⟩───────────────⊕─┼───┼───×───────────────────×─•─┼─┼─┼─⊕─────────────── │ │ │ │ │ │ │ │ │ │ |0⟩─────────────⊕─┼─┼───┼───┼───×───────────×─•─┼─┼─┼─┼─┼─┼─⊕───────────── │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ |0⟩───────────⊕─┼─┼─┼───┼───┼───┼───×───×─•─┼─┼─┼─┼─┼─┼─┼─┼─┼─⊕─────────── │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ |0⟩─•─•─•─•─•─•─•─•─•─•─×─•─×─•─×─•─×─•─×─┼─×─┼─×─┼─×─┼─•─•─•─•─•─•─•─•─•─ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ |0⟩─┼─┼─┼─┼─⊕─────────⊕─•─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─•─⊕─────────⊕─┼─┼─┼─┼─ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ |0⟩─┼─┼─┼─⊕───────────────⊕─•─┼─┼─┼─┼─┼─┼─┼─┼─┼─•─⊕───────────────⊕─┼─┼─┼─ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ |0⟩─┼─┼─⊕─────────────────────⊕─•─┼─┼─┼─┼─┼─•─⊕─────────────────────⊕─┼─┼─ │ │ │ │ │ │ │ │ │ |0⟩─┼─⊕───────────────────────────⊕─•─┼─•─⊕───────────────────────────⊕─┼─ │ │ │ |0⟩─⊕─────────────────────────────────⊕─────────────────────────────────⊕─ '''.strip()
def test_aqt_backend_functional_test(monkeypatch): correct_info = { 'circuit': '[["Y", 0.5, [1]], ["X", 0.5, [1]], ["X", 0.5, [1]], ' '["Y", 0.5, [1]], ["MS", 0.5, [1, 2]], ["X", 3.5, [1]], ' '["Y", 3.5, [1]], ["X", 3.5, [2]]]', 'nq': 3, 'shots': 10, 'backend': { 'name': 'simulator' } } def mock_send(*args, **kwargs): assert args[0] == correct_info return [0, 6, 0, 6, 0, 0, 0, 6, 0, 6] monkeypatch.setattr(_aqt, "send", mock_send) backend = _aqt.AQTBackend(verbose=True, num_runs=10) # no circuit has been executed -> raises exception with pytest.raises(RuntimeError): backend.get_probabilities([]) mapper = BasicMapperEngine() res = dict() for i in range(4): res[i] = i mapper.current_mapping = res eng = MainEngine(backend=backend, engine_list=[mapper]) unused_qubit = eng.allocate_qubit() qureg = eng.allocate_qureg(2) # entangle the qureg Ry(math.pi / 2) | qureg[0] Rx(math.pi / 2) | qureg[0] Rx(math.pi / 2) | qureg[0] Ry(math.pi / 2) | qureg[0] Rxx(math.pi / 2) | (qureg[0], qureg[1]) Rx(7 * math.pi / 2) | qureg[0] Ry(7 * math.pi / 2) | qureg[0] Rx(7 * math.pi / 2) | qureg[1] All(Barrier) | qureg del unused_qubit # measure; should be all-0 or all-1 All(Measure) | qureg # run the circuit eng.flush() prob_dict = eng.backend.get_probabilities([qureg[0], qureg[1]]) assert prob_dict['11'] == pytest.approx(0.4) assert prob_dict['00'] == pytest.approx(0.6) # Unknown qubit and no mapper invalid_qubit = [Qubit(eng, 10)] with pytest.raises(RuntimeError): eng.backend.get_probabilities(invalid_qubit) with pytest.raises(RuntimeError): eng.backend.get_probabilities(eng.allocate_qubit())
def test_deallocate_qubit_exception(): eng = _basics.BasicEngine() qubit = Qubit(eng, -1) with pytest.raises(ValueError): eng.deallocate_qubit(qubit)