def test_hash_function_implemented(): assert hash(AddConstant(3)) == hash(str(AddConstant(3))) assert hash(SubConstant(-3)) == hash(str(AddConstant(3))) assert hash(AddConstantModN(7, 4)) == hash(str(AddConstantModN(7, 4))) assert hash(SubConstantModN(7, 4)) == hash(str(AddConstantModN(-3, 4))) assert hash(MultiplyByConstantModN(3, 5)) == hash( MultiplyByConstantModN(3, 5))
def test_hash_function_implemented(): assert hash(AddConstant(3)) == hash(str(AddConstant(3))) assert hash(SubConstant(-3)) == hash(str(AddConstant(3))) assert hash(AddConstantModN(7, 4)) == hash(str(AddConstantModN(7, 4))) assert hash(SubConstantModN(7, 4)) == hash(str(AddConstantModN(-3, 4))) assert hash(MultiplyByConstantModN(3, 5)) == hash( str(MultiplyByConstantModN(3, 5))) assert hash(AddQuantum) == hash(str(AddQuantum)) assert hash(SubtractQuantum) == hash(str(SubtractQuantum)) assert hash(ComparatorQuantum) == hash(str(ComparatorQuantum)) assert hash(DivideQuantum) == hash(str(DivideQuantum)) assert hash(MultiplyQuantum) == hash(str(MultiplyQuantum))
def test_high_level_gate_set(): mod_list = projectq.setups.ibm16.get_engine_list() saving_engine = DummyEngine(save_commands=True) mod_list = mod_list[:6] + [saving_engine] + mod_list[6:] eng = MainEngine(DummyEngine(), engine_list=mod_list) qureg = eng.allocate_qureg(3) AddConstant(3) | qureg QFT | qureg eng.flush() received_gates = [cmd.gate for cmd in saving_engine.received_commands] assert sum([1 for g in received_gates if g == QFT]) == 1 assert get_inverse(QFT) not in received_gates assert AddConstant(3) not in received_gates
def test_restriction(): engine_list = grid_setup.get_engine_list(num_rows=3, num_columns=2, one_qubit_gates=(Rz, H), two_qubit_gates=(CNOT, AddConstant)) backend = DummyEngine(save_commands=True) eng = projectq.MainEngine(backend, engine_list) qubit1 = eng.allocate_qubit() qubit2 = eng.allocate_qubit() qubit3 = eng.allocate_qubit() eng.flush() CNOT | (qubit1, qubit2) H | qubit1 Rz(0.2) | qubit1 Measure | qubit1 Swap | (qubit1, qubit2) Rx(0.1) | (qubit1) AddConstant(1) | qubit1 + qubit2 + qubit3 eng.flush() assert backend.received_commands[4].gate == X assert len(backend.received_commands[4].control_qubits) == 1 assert backend.received_commands[5].gate == H assert backend.received_commands[6].gate == Rz(0.2) assert backend.received_commands[7].gate == Measure for cmd in backend.received_commands[7:]: assert cmd.gate != Swap assert not isinstance(cmd.gate, Rx) assert not isinstance(cmd.gate, AddConstant)
def test_adder(eng): qureg = eng.allocate_qureg(4) init(eng, qureg, 4) AddConstant(3) | qureg assert 1.0 == pytest.approx(abs(eng.backend.cheat()[1][7])) init(eng, qureg, 7) # reset init(eng, qureg, 2) # check for overflow -> should be 15+2 = 1 (mod 16) AddConstant(15) | qureg assert 1.0 == pytest.approx(abs(eng.backend.cheat()[1][1])) All(Measure) | qureg
def test_adder(): sim = Simulator() eng = MainEngine(sim, [AutoReplacer(), InstructionFilter(no_math_emulation)]) qureg = eng.allocate_qureg(4) init(eng, qureg, 4) AddConstant(3) | qureg assert 1. == pytest.approx(abs(sim.cheat()[1][7])) init(eng, qureg, 7) # reset init(eng, qureg, 2) AddConstant(15) | qureg # check for overflow -> should be 15+2 = 1 (mod 16) assert 1. == pytest.approx(abs(sim.cheat()[1][1])) Measure | qureg
def test_restriction(): engine_list = restrictedgateset.get_engine_list( one_qubit_gates=(Rz, H), two_qubit_gates=(CNOT, AddConstant, Swap), other_gates=(Toffoli, AddConstantModN, MultiplyByConstantModN(2, 8)), ) backend = DummyEngine(save_commands=True) eng = projectq.MainEngine(backend, engine_list, verbose=True) qubit1 = eng.allocate_qubit() qubit2 = eng.allocate_qubit() qubit3 = eng.allocate_qubit() eng.flush() CNOT | (qubit1, qubit2) H | qubit1 with Control(eng, qubit2): Rz(0.2) | qubit1 Measure | qubit1 AddConstant(1) | (qubit1 + qubit2) AddConstantModN(1, 9) | (qubit1 + qubit2 + qubit3) Toffoli | (qubit1 + qubit2, qubit3) Swap | (qubit1, qubit2) MultiplyByConstantModN(2, 8) | qubit1 + qubit2 + qubit3 TimeEvolution(0.5, QubitOperator("X0 Y1 Z2")) | qubit1 + qubit2 + qubit3 QFT | qubit1 + qubit2 + qubit3 Rx(0.1) | (qubit1) MultiplyByConstantModN(2, 9) | qubit1 + qubit2 + qubit3 eng.flush() assert backend.received_commands[4].gate == X assert len(backend.received_commands[4].control_qubits) == 1 assert backend.received_commands[5].gate == H assert backend.received_commands[6].gate == Rz(0.1) assert backend.received_commands[10].gate == Measure assert backend.received_commands[11].gate == AddConstant(1) assert backend.received_commands[12].gate == AddConstantModN(1, 9) assert backend.received_commands[13].gate == X assert len(backend.received_commands[13].control_qubits) == 2 assert backend.received_commands[14].gate == Swap assert backend.received_commands[15].gate == MultiplyByConstantModN(2, 8) for cmd in backend.received_commands[16:]: assert cmd.gate != QFT assert not isinstance(cmd.gate, Rx) assert not isinstance(cmd.gate, MultiplyByConstantModN) assert not isinstance(cmd.gate, TimeEvolution)
def run_simulation(sim): eng = MainEngine(sim, []) quint = eng.allocate_qureg(5) AddConstant(3) | quint All(Measure) | quint eng.flush() results[0].append([int(qb) for qb in quint]) AddConstantModN(4, 5) | quint All(Measure) | quint eng.flush() results[1].append([int(qb) for qb in quint]) MultiplyByConstantModN(15, 16) | quint All(Measure) | quint eng.flush() results[2].append([int(qb) for qb in quint])
def test_addconstant(): assert AddConstant(3) == AddConstant(3) assert not AddConstant(3) == AddConstant(4) assert AddConstant(7) != AddConstant(3) assert str(AddConstant(3)) == "AddConstant(3)"