def test_add_verbatim_box_with_target(cnot): circ = Circuit().add_verbatim_box(cnot, target=[10, 11]) expected = (Circuit().add_instruction( Instruction(compiler_directives.StartVerbatimBox())).add_instruction( Instruction(Gate.CNot(), [10, 11])).add_instruction( Instruction(compiler_directives.EndVerbatimBox()))) assert circ == expected
def test_getters(): target = [0, 1] operator = Gate.CNot() instr = Instruction(operator, target) assert instr.operator == operator assert instr.target == QubitSet([0, 1])
def test_add_circuit_with_target(bell_pair): circ = Circuit().add_circuit(bell_pair, target=[10, 11]) expected = (Circuit().add_instruction(Instruction( Gate.H(), 10)).add_instruction(Instruction(Gate.CNot(), [10, 11])).add_result_type( ResultType.Probability([10, 11]))) assert circ == expected
def test_add_verbatim_box_no_preceding(): circ = Circuit().add_verbatim_box(Circuit().h(0)).cnot(2, 3) expected = (Circuit().add_instruction( Instruction(compiler_directives.StartVerbatimBox())).add_instruction( Instruction(Gate.H(), 0)).add_instruction( Instruction( compiler_directives.EndVerbatimBox())).add_instruction( Instruction(Gate.CNot(), [2, 3]))) assert circ == expected
def test_equality(): instr_1 = Instruction(Gate.H(), 0) instr_2 = Instruction(Gate.H(), 0) other_instr = Instruction(Gate.CNot(), [0, 1]) non_instr = "non instruction" assert instr_1 == instr_2 assert instr_1 is not instr_2 assert instr_1 != other_instr assert instr_1 != non_instr
def test_add_verbatim_box_different_qubits(): circ = Circuit().h(1).add_verbatim_box(Circuit().h(0)).cnot(3, 4) expected = (Circuit().add_instruction(Instruction( Gate.H(), 1)).add_instruction(Instruction( compiler_directives.StartVerbatimBox())).add_instruction( Instruction(Gate.H(), 0)).add_instruction( Instruction( compiler_directives.EndVerbatimBox())).add_instruction( Instruction(Gate.CNot(), [3, 4]))) assert circ == expected
( Gate.PhaseShift(angle=0.17), [4], OpenQASMSerializationProperties( qubit_reference_type=QubitReferenceType.VIRTUAL), "phaseshift(0.17) q[4];", ), ( Gate.PhaseShift(angle=0.17), [4], OpenQASMSerializationProperties( qubit_reference_type=QubitReferenceType.PHYSICAL), "phaseshift(0.17) $4;", ), ( Gate.CNot(), [4, 5], OpenQASMSerializationProperties( qubit_reference_type=QubitReferenceType.VIRTUAL), "cnot q[4], q[5];", ), ( Gate.CNot(), [4, 5], OpenQASMSerializationProperties( qubit_reference_type=QubitReferenceType.PHYSICAL), "cnot $4, $5;", ), ( Gate.PSwap(angle=0.17), [4, 5],
def test_matrix_equivalence(): gate1 = Gate.H() gate2 = Gate.H() gate3 = Gate.CNot() assert gate1.matrix_equivalence(gate2) assert not gate2.matrix_equivalence(gate3)
def cnot(q1, q2): return Instruction(Gate.CNot(), [q1, q2])
def cnot_instr(): return Instruction(Gate.CNot(), [0, 1])
def test_copy_with_target(cnot): target = [10, 11] expected = Instruction(Gate.CNot(), target) assert cnot.copy(target=target) == expected
def test_copy_with_mapping(cnot): target_mapping = {0: 10, 1: 11} expected = Instruction(Gate.CNot(), [10, 11]) assert cnot.copy(target_mapping=target_mapping) == expected
def cnot_unitary(): return Unitary(Gate.CNot().to_matrix())
], [ Instruction(Gate.H, 3), Instruction(h_unitary(), 3), Instruction(i_unitary(), 1), [Instruction(h_unitary(), 0)], ], ), ( cnot_unitary(), [[0, 1]], [ Instruction(cnot_unitary(), [0, 1]), ], [ Instruction(Gate.CNot(), [0, 1]), ], ), ( h_unitary(), 1, [Instruction(h_unitary(), 1)], [Instruction(h_unitary(), 0)], ), ( h_unitary(), None, [Instruction(h_unitary(), 0), Instruction(h_unitary(), 10)], [Instruction(i_unitary(), 1), Instruction(x_unitary(), 3)], ), (
def test_non_matching_qubit_set_and_qubit_count(): Instruction(Gate.CNot(), target=[0, 0])
def test_add_instruction_with_mapping(cnot_instr): expected = [Instruction(Gate.CNot(), [10, 11])] circ = Circuit().add_instruction(cnot_instr, target_mapping={0: 10, 1: 11}) assert list(circ.instructions) == expected
def test_add_instruction_with_target(cnot_instr): expected = [Instruction(Gate.CNot(), [10, 11])] circ = Circuit().add_instruction(cnot_instr, target=[10, 11]) assert list(circ.instructions) == expected
def test_init_with_qubits(): target = QubitSet([0, 1]) instr = Instruction(Gate.CNot(), target) assert instr.target == target
def cnot(): return Circuit().add_instruction(Instruction(Gate.CNot(), [0, 1]))
def test_init_with_sequence(): target = [0, Qubit(1)] instr = Instruction(Gate.CNot(), target) assert instr.target == QubitSet([0, 1])
def bell_pair(prob): return (Circuit().add_instruction(Instruction( Gate.H(), 0)).add_instruction(Instruction(Gate.CNot(), [0, 1])).add_result_type(prob))
) def test_serialization(gates, qubits): test_criteria = GateCriteria(gates=gates, qubits=qubits) serialized_criteria = test_criteria.to_dict() assert serialized_criteria["__class__"] == "GateCriteria" assert "gates" in serialized_criteria assert "qubits" in serialized_criteria deserialized_criteria = GateCriteria.from_dict(serialized_criteria) assert test_criteria == deserialized_criteria @pytest.mark.parametrize( "gates, qubits, matching_instructions, non_matching_instructions", [ (None, 1, [Instruction(Gate.H(), 1) ], [Instruction(Gate.CNot(), [0, 1])]), ( None, None, [Instruction(Gate.H(), 1), Instruction(Gate.CNot(), [0, 1])], [Instruction(Observable.X, 0)], ), ( Gate.H, range(3), [Instruction(Gate.H(), 0), Instruction(Gate.H(), 1)], [ Instruction(Gate.H(), 3), Instruction(Gate.I(), 1),