def step_1(): qc = QuantumCircuit(3) qc.x(0) qc.h(2) qc.h(1) qc.h(0) draw_quantum_circuit(qc, draw_bloch_sphere=False)
def measure_qc(): qc = QuantumCircuit(8) qc.x(7) draw_quantum_circuit(qc, draw_circuit=True, draw_unitary=False, draw_final_state=False, draw_bloch_sphere=True, draw_histogram=True)
def cnot_behavior(): """ Show that the following quantum circuits are equivalent: ┌───┐ ┌───┐ ┌───┐ q_0: ┤ X ├ q_0: ┤ H ├──■──┤ H ├ └─┬─┘ ├───┤┌─┴─┐├───┤ q_1: ──■── q_1: ┤ H ├┤ X ├┤ H ├ └───┘└───┘└───┘ Unitary of both: 1 0 0 0 0 1 0 0 0 0 0 1 0 0 1 0 """ qc_1 = QuantumCircuit(2) qc_1.cx(1, 0) draw_quantum_circuit(qc_1, draw_final_state=0, draw_unitary=1) qc_2 = QuantumCircuit(2) qc_2.h(0) qc_2.h(1) qc_2.cx(0, 1) qc_2.h(0) qc_2.h(1) draw_quantum_circuit(qc_2, draw_final_state=0, draw_unitary=1)
def balanced_oracle(draw=False): n = 3 qc = QuantumCircuit(n + 1) # Place X-gates bit_str = '' for i in range(n): if np.random.randint(2): bit_str += '1' else: bit_str += '0' for qubit, qubit_value in enumerate(bit_str): if qubit_value == '1': qc.x(qubit) qc.barrier() # Place CNOTs for qubit_value in range(n): qc.cx(qubit_value, n) qc.barrier() # Place X gates for qubit, qubit_value in enumerate(bit_str): if qubit_value == '1': qc.x(qubit) print('Output:', bit_str) if draw: draw_quantum_circuit(qc, draw_bloch_sphere=False) return qc
def superposition(): qc = QuantumCircuit(2) qc.h(0) qc.x(1) qc.h(1) qc.cnot(0, 1) draw_quantum_circuit(qc)
def execute_dj(): n = 4 oracle = dj_oracle('balanced', n) dj_circuit = dj_algorithm(oracle, n) draw_quantum_circuit(dj_circuit, draw_final_state=False, draw_bloch_sphere=False, draw_histogram=True) backend = least_busy_backend(n) run_quantum_job(backend, dj_circuit)
def exercise_1_3_1(): qc = QuantumCircuit(1) initial_state = normalize([1, sqrt(2)]) initial_state = normalize([1, 1j * sqrt(2)]) print(initial_state) qc.initialize(initial_state, 0) draw_quantum_circuit(qc, draw_unitary=False, draw_histogram=True)
def deutsch_jozsa(): n = 3 dj_circuit = QuantumCircuit(n + 1, n) # Apply H-gates for qubit in range(n): dj_circuit.h(qubit) # Put qubit in state |-> dj_circuit.x(n) dj_circuit.h(n) # Apply an oracle # dj_circuit += balanced_oracle() dj_circuit += constant_oracle() # Repeat H-gates for qubit in range(n): dj_circuit.h(qubit) dj_circuit.barrier() # Measure for i in range(n): dj_circuit.measure(i, i) draw_quantum_circuit(dj_circuit, draw_final_state=False, draw_bloch_sphere=False, draw_histogram=True)
def cnot_conjugation(): with warnings.catch_warnings(): warnings.simplefilter("ignore") q = QuantumCircuit(2) q.cnot(1, 0) q.x(1) q.cnot(1, 0) draw_quantum_circuit(q, draw_circuit=False, draw_bloch_sphere=False, draw_final_state=False)
def controlled_rotation_y(): qc = QuantumCircuit(2) theta = pi # theta can be anything (pi chosen arbitrarily) qc.ry(theta / 2, 1) qc.cx(0, 1) qc.ry(-theta / 2, 1) qc.cx(0, 1) draw_quantum_circuit(qc, draw_final_state=False, draw_bloch_sphere=False)
def first_cnot(): qc = QuantumCircuit(2) # Apply H-gate to the first: qc.h(0) # qc.x(1) qc.cx(0, 1) draw_quantum_circuit(qc, draw_histogram=True)
def build_controlled_rotation(): qc = QuantumCircuit(3) theta = pi / 4 # chosen arbitrarily qc.cp(theta / 2, 1, 2) qc.cx(0, 1) qc.cp(-theta / 2, 1, 2) qc.cx(0, 1) qc.cp(theta / 2, 0, 2) draw_quantum_circuit(qc, draw_final_state=False, draw_bloch_sphere=False)
def swap_bits(): qc = QuantumCircuit(2) qc.swap(0, 1) draw_quantum_circuit(qc, draw_final_state=False, draw_bloch_sphere=False) qc = QuantumCircuit(2) qc.cnot(0, 1) qc.cnot(1, 0) qc.cnot(0, 1) draw_quantum_circuit(qc, draw_final_state=False, draw_bloch_sphere=False)
def single_cp(): qc = QuantumCircuit(1) theta = pi / 4 qc.p(theta, 0) draw_quantum_circuit(qc) qc = QuantumCircuit(2) theta = pi / 4 qc.cp(theta, 0, 1) draw_quantum_circuit(qc)
def controlled_h(): qc = QuantumCircuit(2) qc.ch(0, 1) draw_quantum_circuit(qc, draw_final_state=False, draw_bloch_sphere=False) qc = QuantumCircuit(2) qc.ry(pi / 4, 1) qc.cnot(0, 1) qc.ry(-pi / 4, 1) draw_quantum_circuit(qc, draw_final_state=False, draw_bloch_sphere=False)
def controlled_y(): qc = QuantumCircuit(2) qc.cy(0, 1) draw_quantum_circuit(qc, draw_final_state=False, draw_bloch_sphere=False) qc = QuantumCircuit(2) qc.sdg(1) qc.cnot(0, 1) qc.s(1) draw_quantum_circuit(qc, draw_final_state=False, draw_bloch_sphere=False)
def h(): for state in choices: print(f'H |{state.value}>') qc = QuantumCircuit(1) bit_to(qc, 0, state) qc.h(0) draw_quantum_circuit(qc, draw_circuit=False, draw_unitary=False, draw_bloch_sphere=False) print('----')
def draw_bloch_spheres(): qc = QuantumCircuit(1) states = [ [1, 0], [0, 1], [1, 1], [1, -1j], [1j, 1], ] for state in states: qc.initialize(normalize(state)) draw_quantum_circuit(qc, draw_unitary=False)
def cnot(): for state_0 in choices: for state_1 in choices: print(f'CNOT |{state_1.value}{state_0.value}>') qc = QuantumCircuit(2) bit_to(qc, 0, state_0) bit_to(qc, 1, state_1) qc.cnot(0, 1) draw_quantum_circuit(qc, draw_circuit=False, draw_unitary=False, draw_bloch_sphere=False) print('----')
def initialize(): qc = QuantumCircuit(1) # Define initial_state as |1> initial_state = [0, 1] # Define state |q_0> initial_state = [1 / sqrt(2), 1j / sqrt(2)] # Apply initialisation operation to the 0th qubit qc.initialize(initial_state, 0) draw_quantum_circuit(qc, draw_unitary=False)
def constant_oracle(draw=False): n = 3 qc = QuantumCircuit(n + 1) # Place X-gates output = np.random.randint(2) if output == 1: qc.x(n) print('Output:', output) if draw: draw_quantum_circuit(qc, draw_bloch_sphere=False) return qc
def single_states(): """ Print the definitions of the single state eigenvectors """ for state in choices: qc = QuantumCircuit(1) print(f'|{state.value}>') bit_to(qc, 0, state) draw_quantum_circuit(qc, draw_circuit=False, draw_unitary=False, draw_bloch_sphere=False) print('----')
def xh(): """ Proof that X x H = 0 0 0.71 0.71 0 0 0.71 -0.71 0.71 0.71 0 0 0.71 -0.71 0 0 And hence that X x H means X\q1> x H|q0> """ qc = QuantumCircuit(2) qc.h(0) qc.x(1) draw_quantum_circuit(qc)
def hxh_gate(): """ I have calculated that HZH = I. This checks whether that is correct """ for initial_state in [state_z0, state_z1, state_x0, state_x1, state_y0, state_y1]: qc = QuantumCircuit(1, 1) qc.initialize(initial_state, 0) qc.h(0) qc.x(0) qc.h(0) draw_quantum_circuit(qc, draw_circuit=False, draw_bloch_sphere=False, draw_unitary=False)
def dual_states(): """ Print the definitions of the combinations of all single-state eigenvectors """ for state_0 in choices: for state_1 in choices: print(f'|{state_1.value}{state_0.value}>') qc = QuantumCircuit(2) bit_to(qc, 0, state_0) bit_to(qc, 1, state_1) draw_quantum_circuit(qc, draw_circuit=False, draw_unitary=False, draw_bloch_sphere=False) print('----')
def half_adder(): qc = QuantumCircuit(4, 2) qc.x(0) # qc.x(1) qc.barrier() # Set bit 2: 0 for 00 and 11, 1 for 10 and 01 qc.cx(0, 2) qc.cx(1, 2) # Set bit 3: 0 for 00, 01 and 10, 1 for 11 qc.ccx(0, 1, 3) qc.barrier() qc.measure(2, 0) qc.measure(3, 1) draw_quantum_circuit(qc, draw_circuit=True, draw_unitary=False, draw_final_state=False, draw_bloch_sphere=True, draw_histogram=True)
def t(): """ The T-gate is the controlled U gate with ( u_00 u_01 ) ( 0 0 ) U = ( u_10 u_11 ) = ( 0 exp(i pi /4) """ for state_0 in choices: for state_1 in choices: print(f'T |{state_0.value}{state_1.value}>') qc = QuantumCircuit(2) bit_to(qc, 0, state_0) bit_to(qc, 1, state_1) qc.cu1(pi / 4, 1, 0) draw_quantum_circuit(qc, draw_circuit=False, draw_unitary=True, draw_bloch_sphere=False) print('----')
def build_toffoli(): qc = QuantumCircuit(3) qc.h(2) qc.cx(1, 2) qc.tdg(2) qc.cx(0, 2) qc.t(2) qc.cx(1, 2) qc.tdg(2) qc.cx(0, 2) qc.t(1) qc.t(2) qc.h(2) qc.cx(0, 1) qc.t(0) qc.tdg(1) qc.cx(0, 1) draw_quantum_circuit(qc) qc = QuantumCircuit(3) qc.ch(0, 2) qc.cz(1, 2) qc.ch(0, 2) draw_quantum_circuit(qc)
def build_t_gates(): # Rotate pi/4 along the Z-axis qc = QuantumCircuit(1) qc.t(0) draw_quantum_circuit(qc) # Rotate pi/4 along the X-axis qc = QuantumCircuit(1) qc.h(0) qc.t(0) qc.h(0) draw_quantum_circuit(qc) # Rotate pi/4 along the X-axis, then around the Z-axis qc = QuantumCircuit(1) qc.h(0) qc.t(0) qc.h(0) qc.t(0) draw_quantum_circuit(qc)
def make_oracle_balanced(): qc = QuantumCircuit(4) qc.cnot(0, 1) qc.cnot(0, 2) qc.cnot(0, 3) draw_quantum_circuit(qc, draw_bloch_sphere=False)