def reset(self, prog: Program): """ Reset the physical qubits to the |0>^{\otimes n} state and the errors to 0. This code block must not be entangled with any other qubits in the system. """ prog += (gates.MEASURE(self.qubits[i], self.x_errors[i]) for i in range(self.n)) for i in range(self.n): prog.if_then(self.x_errors[i], gates.X(self.qubits[i])) prog += gates.MOVE(self.x_errors[i], 0) prog += gates.MOVE(self.z_errors[i], 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()])
def test_if_then_2(): # if FALSE creg, then measure 0 should give 1 prog = Program() creg = prog.declare("creg", "BIT") prog.inst(MOVE(creg, 0), X(0)) branch_a = Program(X(0)) branch_b = Program() prog.if_then(creg, branch_a, branch_b) prog += MEASURE(0, creg) qam = PyQVM(n_qubits=1, quantum_simulator_type=ReferenceWavefunctionSimulator) qam.execute(prog) assert qam.ram["creg"][0] == 1
def test_if_then(): # if TRUE creg, then measure 0 should give 0 prog = Program() creg = prog.declare('creg', 'BIT') prog.inst(MOVE(creg, 1), X(0)) branch_a = Program(X(0)) branch_b = Program() prog.if_then(creg, branch_a, branch_b) prog += MEASURE(0, creg) qam = PyQVM(n_qubits=1, quantum_simulator_type=ReferenceWavefunctionSimulator) qam.execute(prog) assert qam.ram['creg'][0] == 0
def test_multiple_measurements_program(self): raw_prog = Program() ro = raw_prog.declare('ro', 'BIT', 2) raw_prog += gates.H(0) raw_prog += gates.MEASURE(0, ro[0]) raw_prog.if_then(ro[0], gates.X(0), Program()) raw_prog += gates.MEASURE(0, ro[1]) new_prog = ftqc.rewrite_program(raw_prog, self.steane_7bit) results = self.run_program(new_prog) for result in results: self.assertEqual(result[1], 0)
def test_if_then(qvm): main = Program().inst(X(0)) branch_a = Program().inst(X(0)) branch_b = Program().inst() creg = 0 main.if_then(creg, branch_a, branch_b) # if TRUE creg, then measure 0 should give 0 prep = Program().inst(TRUE(0)) prog = prep + main assert qvm.run_and_measure(prog, [0])[0][0] == 0 # if FALSE creg, then measure 0 should give 1 prep = Program().inst(FALSE(0)) prog = prep + main assert qvm.run_and_measure(prog, [0])[0][0] == 1
def test_if_then_inherits_defined_gates(): p1 = Program() p1.inst(H(0)) p1.measure(0, MemoryReference("ro", 0)) p2 = Program() p2.defgate("A", np.array([[1.0, 0.0], [0.0, 1.0]])) p2.inst(("A", 0)) p3 = Program() p3.defgate("B", np.array([[0.0, 1.0], [1.0, 0.0]])) p3.inst(("B", 0)) p1.if_then(MemoryReference("ro", 0), p2, p3) assert p2.defined_gates[0] in p1.defined_gates assert p3.defined_gates[0] in p1.defined_gates
def test_if_then_inherits_defined_gates(): p1 = Program() p1.inst(H(0)) p1.measure(0, 0) p2 = Program() p2.defgate("A", np.array([[1., 0.], [0., 1.]])) p2.inst(("A", 0)) p3 = Program() p3.defgate("B", np.array([[0., 1.], [1., 0.]])) p3.inst(("B", 0)) p1.if_then(0, p2, p3) assert p2.defined_gates[0] in p1.defined_gates assert p3.defined_gates[0] in p1.defined_gates
def test_len_nested(): p = Program(Declare("ro", "BIT"), H(0)).measure(0, MemoryReference("ro", 0)) q = Program(H(0), CNOT(0, 1)) p.if_then(MemoryReference("ro", 0), q) assert len(p) == 9
def test_nesting_a_program_inside_itself(): p = Program(H(0)).measure(0, MemoryReference("ro", 0)) with pytest.raises(ValueError): p.if_then(MemoryReference("ro", 0), p)
def test_nesting_a_program_inside_itself(): p = Program(H(0)).measure(0, 0) with pytest.raises(ValueError): p.if_then(0, p)
def test_len_nested(): p = Program(H(0)).measure(0, 0) q = Program(H(0), CNOT(0, 1)) p.if_then(0, q) assert len(p) == 8
# teleportation circuit # ============================================================================= # Alice wants to send |1> to Bob qprog += gates.X(0) # main circuit qprog += [ gates.H(1), gates.CNOT(1, 2), gates.CNOT(0, 1), gates.H(0), gates.MEASURE(0, creg[0]), gates.MEASURE(1, creg[1]) ] # conditional operations qprog.if_then(creg[0], gates.Z(2)) qprog.if_then(creg[1], gates.X(2)) # measure qubit three qprog.measure(2, creg[2]) # ============================================================================= # run the circuit and print the results. Note Bob always measures 1 # ============================================================================= print(qvm.run(qprog)) # print the quil code print(qprog)
def forward_prop(weights, initialization=None): LOGGER.info("Connecting to the QVM...") qvm = QVMConnection() LOGGER.info("... done") LOGGER.info(" ") LOGGER.info("Initialising quantum program...") p = Program() LOGGER.info("... defining custom gates") LOGGER.info("... controlled Ry") CRY = controlled_Ry(p) LOGGER.info("... controlled sY") CSY = controlled_sY(p) LOGGER.info("... done") LOGGER.info(" ") a = -1 rotation = 0.5 * np.pi * (a + 1) gamma = 1 / NUM_INPUT W1 = weights[:NUM_INPUT * NUM_HIDDEN].reshape((NUM_INPUT, NUM_HIDDEN)) b1 = weights[NUM_INPUT * NUM_HIDDEN] W2 = weights[NUM_INPUT * NUM_HIDDEN + 1:NUM_INPUT * NUM_HIDDEN + 1 + NUM_HIDDEN * NUM_OUTPUT].reshape(NUM_HIDDEN, NUM_OUTPUT) b2 = weights[-1] # INITIALISE INPUT if initialization == None: EDI = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]) dg = DefGate("EDI", EDI) EDI = dg.get_constructor() p.inst(dg) p.inst(H(0)) p.inst(H(1)) p.inst(EDI(0, 1)) if initialization == "test": # p.inst(X(1)) # |01> p.inst(X(0)) # |10> # pass # INTIALISE LABELS p.inst(H(NUM_INPUT + NUM_HIDDEN + NUM_OUTPUT)) classical_flag_register = ANCILLARY_BIT + 1 # Write out the loop initialization and body programs: for n in range(NUM_HIDDEN): loop_body = Program() for w in range(NUM_INPUT): loop_body.inst(CRY(4. * gamma * W1[w, n])(w, ANCILLARY_BIT)) loop_body.inst(RY(2. * gamma * b1)(ANCILLARY_BIT)) loop_body.inst(CSY(ANCILLARY_BIT, NUM_INPUT + n)) loop_body.inst(RZ(-0.5 * np.pi)(ANCILLARY_BIT)) for w in range(NUM_INPUT): loop_body.inst(CRY(-4. * gamma * W1[w, n])(w, ANCILLARY_BIT)) loop_body.inst(RY(-2. * gamma * b1)(ANCILLARY_BIT)) loop_body.measure(ANCILLARY_BIT, classical_flag_register) then_branch = Program(RY(-0.5 * np.pi)(NUM_INPUT + n)) then_branch.inst(X(ANCILLARY_BIT)) else_branch = Program() # Add the conditional branching: loop_body.if_then(classical_flag_register, then_branch, else_branch) init_register = Program(TRUE([classical_flag_register])) loop_prog = init_register.while_do(classical_flag_register, loop_body) p.inst(loop_prog) # Write out the loop initialization and body programs: for n in range(NUM_OUTPUT): loop_body = Program() for w in range(NUM_HIDDEN): loop_body.inst(CRY(4. * gamma * W2[w, n])(w, ANCILLARY_BIT)) loop_body.inst(RY(2. * gamma * b2)(ANCILLARY_BIT)) loop_body.inst(CSY(ANCILLARY_BIT, NUM_INPUT + NUM_HIDDEN + n)) loop_body.inst(RZ(-0.5 * np.pi)(ANCILLARY_BIT)) for w in range(NUM_HIDDEN): loop_body.inst(CRY(-4. * gamma * W2[w, n])(w, ANCILLARY_BIT)) loop_body.inst(RY(-2. * gamma * b1)(ANCILLARY_BIT)) loop_body.measure(ANCILLARY_BIT, classical_flag_register) then_branch = Program(RY(-0.5 * np.pi)(NUM_INPUT + NUM_HIDDEN + n)) then_branch.inst(X(ANCILLARY_BIT)) else_branch = Program() # Add the conditional branching: loop_body.if_then(classical_flag_register, then_branch, else_branch) init_register = Program(TRUE([classical_flag_register])) loop_prog = init_register.while_do(classical_flag_register, loop_body) p.inst(loop_prog) p.measure(NUM_INPUT + NUM_HIDDEN, 0) LOGGER.info("... executing on the QVM") classical_regs = [0] output = qvm.run(p, classical_regs) LOGGER.info("... %s", output) LOGGER.info("") return output[0][0]