示例#1
0
def test_extract_qubits():
    p = Program(RX(0.5)(0), RY(0.1)(1), RZ(1.4)(2))
    assert p.extract_qubits() == set([0, 1, 2])
    p.if_then(0, X(4), H(5)).measure(6, 2)
    assert p.extract_qubits() == set([0, 1, 2, 4, 5, 6])
    p.while_do(0, Program(X(3)).measure(3, 0))
    assert p.extract_qubits() == set([0, 1, 2, 3, 4, 5, 6])
    new_qubit = p.alloc()
    p.inst(X(new_qubit))
    p.synthesize()
    assert p.extract_qubits() == set([0, 1, 2, 3, 4, 5, 6, new_qubit.index()])
示例#2
0
def test_1_qubit_control():
    prog = Program()
    qubit = prog.alloc()
    control_qubit = prog.alloc()
    prog += (ControlledProgramBuilder().with_controls([
        control_qubit
    ]).with_target(qubit).with_operation(SIGMA_Z).with_gate_name("Z").build())
    # This should be one "CZ" instruction, from control_qubit to qubit.
    assert prog_len(prog) == 1
    prog.synthesize()
    instruction = non_action_insts(prog)[0]
    assert instruction[1].operator_name == 'C[Z]'
    assert instruction[1].arguments == [control_qubit, qubit]
示例#3
0
def test_2_qubit_control():
    """Test that ControlledProgramBuilder builds the program correctly all the way through."""
    prog = Program()
    qubit = prog.alloc()
    control_qubit_one = prog.alloc()
    control_qubit_two = prog.alloc()
    prog += (ControlledProgramBuilder().with_controls([
        control_qubit_one, control_qubit_two
    ]).with_target(qubit).with_operation(SIGMA_Z).with_gate_name("Z").build())
    # This should be one "CZ" instruction, from control_qubit to qubit.
    assert prog_len(prog) == 5
    prog.synthesize()
    instructions = non_action_insts(prog)
    # Run tests
    double_control_test(instructions, qubit, control_qubit_one,
                        control_qubit_two)
示例#4
0
def test_trivial_grover():
    """Testing that we construct the correct circuit for Grover's Algorithm with one step, and the
     identity_oracle on one qubit.
     """
    trivial_grover = Program()
    qubit0 = trivial_grover.alloc()
    # First we put the input into uniform superposition.
    trivial_grover.inst(H(qubit0))
    # No oracle is applied, so we just apply the diffusion operator.
    trivial_grover.inst(H(qubit0))
    trivial_grover.inst(Z(qubit0))
    trivial_grover.inst(H(qubit0))
    qubits = [qubit0]
    generated_trivial_grover = Grover().oracle_grover(identity_oracle, qubits,
                                                      1)
    generated_trivial_grover.synthesize()
    trivial_grover.synthesize()
    assert prog_equality(generated_trivial_grover, trivial_grover)