def test_decomposition(gate_matrix): # Create single qubit gate with gate_matrix test_gate = MatrixGate() test_gate.matrix = np.matrix(gate_matrix) for basis_state in ([1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]): correct_dummy_eng = DummyEngine(save_commands=True) correct_eng = MainEngine(backend=Simulator(), engine_list=[correct_dummy_eng]) rule_set = DecompositionRuleSet(modules=[carb1q]) test_dummy_eng = DummyEngine(save_commands=True) test_eng = MainEngine( backend=Simulator(), engine_list=[ AutoReplacer(rule_set), InstructionFilter(_decomp_gates), test_dummy_eng, ], ) test_sim = test_eng.backend correct_sim = correct_eng.backend correct_qb = correct_eng.allocate_qubit() correct_ctrl_qb = correct_eng.allocate_qubit() correct_eng.flush() test_qb = test_eng.allocate_qubit() test_ctrl_qb = test_eng.allocate_qubit() test_eng.flush() correct_sim.set_wavefunction(basis_state, correct_qb + correct_ctrl_qb) test_sim.set_wavefunction(basis_state, test_qb + test_ctrl_qb) with Control(test_eng, test_ctrl_qb): test_gate | test_qb with Control(correct_eng, correct_ctrl_qb): test_gate | correct_qb test_eng.flush() correct_eng.flush() assert correct_dummy_eng.received_commands[3].gate == test_gate assert test_dummy_eng.received_commands[3].gate != test_gate for fstate in ['00', '01', '10', '11']: test = test_sim.get_amplitude(fstate, test_qb + test_ctrl_qb) correct = correct_sim.get_amplitude(fstate, correct_qb + correct_ctrl_qb) assert correct == pytest.approx(test, rel=1e-12, abs=1e-12) All(Measure) | test_qb + test_ctrl_qb All(Measure) | correct_qb + correct_ctrl_qb test_eng.flush(deallocate_qubits=True) correct_eng.flush(deallocate_qubits=True)
def test_decomposition_errors(gate_matrix): test_gate = MatrixGate() test_gate.matrix = np.matrix(gate_matrix) rule_set = DecompositionRuleSet(modules=[arb1q]) eng = MainEngine(backend=DummyEngine(), engine_list=[ AutoReplacer(rule_set), InstructionFilter(z_y_decomp_gates) ]) qb = eng.allocate_qubit() with pytest.raises(Exception): test_gate | qb
def test_recognize_incorrect_gates(): saving_backend = DummyEngine(save_commands=True) eng = MainEngine(backend=saving_backend) qubit = eng.allocate_qubit() # Does not have matrix attribute: BasicGate() | qubit # Two qubit gate: two_qubit_gate = MatrixGate() two_qubit_gate.matrix = [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]] two_qubit_gate | qubit # Controlled single qubit gate: ctrl_qubit = eng.allocate_qubit() with Control(eng, ctrl_qubit): Rz(0.1) | qubit eng.flush(deallocate_qubits=True) for cmd in saving_backend.received_commands: assert not arb1q._recognize_arb1qubit(cmd)
def test_decomposition(gate_matrix): for basis_state in ([1, 0], [0, 1]): # Create single qubit gate with gate_matrix test_gate = MatrixGate() test_gate.matrix = np.matrix(gate_matrix) correct_dummy_eng = DummyEngine(save_commands=True) correct_eng = MainEngine(backend=Simulator(), engine_list=[correct_dummy_eng]) rule_set = DecompositionRuleSet(modules=[arb1q]) test_dummy_eng = DummyEngine(save_commands=True) test_eng = MainEngine(backend=Simulator(), engine_list=[ AutoReplacer(rule_set), InstructionFilter(z_y_decomp_gates), test_dummy_eng ]) correct_qb = correct_eng.allocate_qubit() correct_eng.flush() test_qb = test_eng.allocate_qubit() test_eng.flush() correct_eng.backend.set_wavefunction(basis_state, correct_qb) test_eng.backend.set_wavefunction(basis_state, test_qb) test_gate | test_qb test_gate | correct_qb test_eng.flush() correct_eng.flush() assert correct_dummy_eng.received_commands[2].gate == test_gate assert test_dummy_eng.received_commands[2].gate != test_gate for fstate in ['0', '1']: test = test_eng.backend.get_amplitude(fstate, test_qb) correct = correct_eng.backend.get_amplitude(fstate, correct_qb) assert correct == pytest.approx(test, rel=1e-12, abs=1e-12) Measure | test_qb Measure | correct_qb
def test_recognize_incorrect_gates(): saving_backend = DummyEngine(save_commands=True) eng = MainEngine(backend=saving_backend) qubit = eng.allocate_qubit() ctrl_qubit = eng.allocate_qubit() ctrl_qureg = eng.allocate_qureg(2) eng.flush() with Control(eng, ctrl_qubit): # Does not have matrix attribute: BasicGate() | qubit # Two qubit gate: two_qubit_gate = MatrixGate() two_qubit_gate.matrix = np.matrix([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]) two_qubit_gate | qubit with Control(eng, ctrl_qureg): # Too many Control qubits: Rx(0.3) | qubit eng.flush(deallocate_qubits=True) for cmd in saving_backend.received_commands: assert not carb1q._recognize_carb1qubit(cmd)