def test_eval_order(self): c = Circuit(2) f = AdditionGate("F") g = AdditionGate("G") h = ScalarMultGate(2, "H") c.add_gate(h, ["INPUT1"]) c.add_gate(f, ["H","INPUT0"]) c.add_gate(g, ["F", "H"], True) self.assertEqual(c.eval_order(), ['H', 'F', 'G', 'OUTPUT'])
def test_evaluation(self): c = Circuit(2) f = AdditionGate("F") g = AdditionGate("G") h = ScalarMultGate(2, "H") c.add_gate(h, ["INPUT1"]) c.add_gate(f, ["H","INPUT0"]) c.add_gate(g, ["F", "H"], True) input0, input1 = 1, 100 result = c.evaluate((input0, input1)) h_res = input1 * 2 f_res = h_res + input0 g_res = h_res + f_res self.assertEqual(result, g_res)
if __name__ == '__main__': N = input('Enter the number of items in the search space: ') n = int(np.ceil(np.log2(N)) + 1) M = input('Enter the number of search targets: ') targets = [] for i in range(M): target = raw_input('Target %d in binary: ' % (i + 1)) targets.append(target) num_iter = input('Number of iterations: ') initial_state = tensor(plus, n) circuit = Circuit(n, initial_state) # Apply the Z gate to the ancilla bit to make it |-> for phase kickback # Multiply by root 2 to renormalize without considering the ancilla qubit state = np.sqrt(2) * circuit.add_gate(Z, [ n, ]) # Switch to interactive mode plt.ion() # Plot the initial probability amplitudes and angle amps_fig = plt.figure(0) amps = amps_fig.add_subplot(111) amps.bar(np.arange(state.size / 2) + 0.1, abs(state[::2])**2) amps_fig.show() angles_fig = plt.figure(1) angles = angles_fig.add_subplot(111, polar=True) theta = np.arccos(np.sqrt((2.0**(n - 1) - M) / (2**(n - 1)))) angles.plot([theta, theta], [0, 1], 'g-', linewidth=2) angles.set_yticks(()) angles_fig.show()
if __name__ == "__main__": N = input("Enter the number of items in the search space: ") n = int(np.ceil(np.log2(N)) + 1) M = input("Enter the number of search targets: ") targets = [] for i in range(M): target = raw_input("Target %d in binary: " % (i + 1)) targets.append(target) num_iter = input("Number of iterations: ") initial_state = tensor(plus, n) circuit = Circuit(n, initial_state) # Apply the Z gate to the ancilla bit to make it |-> for phase kickback # Multiply by root 2 to renormalize without considering the ancilla qubit state = np.sqrt(2) * circuit.add_gate(Z, [n]) # Switch to interactive mode plt.ion() # Plot the initial probability amplitudes and angle amps_fig = plt.figure(0) amps = amps_fig.add_subplot(111) amps.bar(np.arange(state.size / 2) + 0.1, abs(state[::2]) ** 2) amps_fig.show() angles_fig = plt.figure(1) angles = angles_fig.add_subplot(111, polar=True) theta = np.arccos(np.sqrt((2.0 ** (n - 1) - M) / (2 ** (n - 1)))) angles.plot([theta, theta], [0, 1], "g-", linewidth=2) angles.set_yticks(()) angles_fig.show() raw_input("Press any key to continue...")
def qub_xz(params): return (np.pi/2, np.pi/2, -np.pi/2) # rotate qubit from Z to Y def qub_zy(params): return (np.pi/2, 0, np.pi/2) # rotate qubit from Y to Z def qub_yz(params): return (np.pi/2, 0, -np.pi/2) # SNAP equivalent of exp(i theta ZZ) def snap_zz(params): theta = params[0] return [theta, -theta] if __name__ == "__main__": c = Circuit([("qubit", "p", 2), ("cavity", "b", 2)]) # arbitrary one-qubit rotations c.add_gate("rotation") c.add_gate("displacement", n_params=1, fn=cav_xrot) c.add_gate("displacement", n_params=1, fn=cav_yrot) c.add_gate("displacement", n_params=1, fn=cav_xrot) # XX rotation c.add_gate("rotation", n_params=0, fn=qub_xz) c.add_gate("displacement", n_params=0, fn=cav_xz) c.add_gate("snap", n_params=1, fn=snap_zz) c.add_gate("rotation", n_params=0, fn=qub_zx) c.add_gate("displacement", n_params=0, fn=cav_zx) # YY rotation c.add_gate("rotation", n_params=0, fn=qub_yz) c.add_gate("displacement", n_params=0, fn=cav_yz) c.add_gate("snap", n_params=1, fn=snap_zz)