def test_generate_unitary_gate(): assert generate_unitary_gate("X") == XGate() assert generate_unitary_gate("Y") == YGate() assert generate_unitary_gate("S") == SGate() assert generate_unitary_gate("Z") == ZGate() assert generate_unitary_gate("H") == HGate() assert generate_unitary_gate("T") == TGate() assert generate_unitary_gate("I") == IdGate() for i in np.arange(-10, -1, 0.1): for j in np.arange(1, 10, 0.1): # Rx Gates assert generate_unitary_gate(f'Rx(pi*{j})') == RXGate(np.pi * j) assert generate_unitary_gate(f'Rx({i}/ {j}*pi)') == RXGate(i / j * np.pi) assert generate_unitary_gate(f'Rx( {i}*{j}/pi )') == RXGate(i * j / np.pi) assert generate_unitary_gate(f'Rx( {i} / {j}/pi)') == RXGate( i / j / np.pi) # Ry Gates assert generate_unitary_gate(f'Ry(pi*{j})') == RYGate(np.pi * j) assert generate_unitary_gate(f'Ry({i}/ {j}*pi)') == RYGate(i / j * np.pi) assert generate_unitary_gate(f'Ry( {i}*{j}/pi )') == RYGate(i * j / np.pi) assert generate_unitary_gate(f'Ry( {i} / {j}/pi)') == RYGate( i / j / np.pi) # Rz Gates assert generate_unitary_gate(f'Rz(pi*{j})') == RZGate(np.pi * j) assert generate_unitary_gate(f'Rz({i}/ {j}*pi)') == RZGate(i / j * np.pi) assert generate_unitary_gate(f'Rz( {i}*{j}/pi )') == RZGate(i * j / np.pi) assert generate_unitary_gate(f'Rz( {i} / {j}/pi)') == RZGate( i / j / np.pi)
def crear_circuito(n, tipo): I_f = np.array([[1, 0], [0, 1]]) I = np.array([[1, 0], [0, 1]]) X_f = np.array([[0, 1], [1, 0]]) X = np.array([[0, 1], [1, 0]]) for q in range(n - 1): I_f = np.kron(I_f, I) X_f = np.kron(X_f, X) J = Operator(1 / np.sqrt(2) * (I_f + 1j * X_f)) J_dg = J.adjoint() circ = QuantumCircuit(n, n) circ.append(J, range(n)) if n == 1: dx = np.pi dy = 0 dz = 0 elif n == 2: # barrido dx = tipo[0] dy = tipo[1] dz = tipo[2] for q in range(n): circ.append(RXGate(dx), [q]) circ.append(RYGate(dy), [q]) circ.append(RZGate(dz), [q]) circ.append(J_dg, range(n)) circ.measure(range(n), range(n)) return circ
def output_state(dx,dy,dz): I_f = np.array([[1, 0], [0, 1]]) I = np.array([[1, 0], [0, 1]]) X_f = np.array([[0, 1], [1, 0]]) X = np.array([[0, 1], [1, 0]]) for q in range(1): I_f = np.kron(I_f, I) X_f = np.kron(X_f, X) J = Operator(1 / np.sqrt(2) * (I_f + 1j * X_f)) J_dg = J.adjoint() circ = QuantumCircuit(2,2) circ.append(J, range(2)) for q in range(2): circ.append(RXGate(dx),[q]) circ.append(RYGate(dy),[q]) circ.append(RZGate(dz),[q]) circ.append(J_dg, range(2)) backend = Aer.get_backend('statevector_simulator') job = backend.run(circ) result = job.result() outputstate = result.get_statevector(circ, decimals=5) return outputstate
def generate_unitary_gate(gate_name: str) -> Gate: # Rx, Ry and Rz gates that look like 'Rx(pi/2) if gate_name[0] == 'R' and gate_name[2] == '(': angle = parse_angle(gate_name[3:-1]) if gate_name[1] == 'x': return RXGate(angle) elif gate_name[1] == 'y': return RYGate(angle) elif gate_name[1] == 'z': return RZGate(angle) else: unitary_gates = { "X": XGate(), "Y": YGate(), "S": SGate(), "Z": ZGate(), "H": HGate(), "T": TGate(), "I": IGate(), "W": WGate(), "Rz1": RZGate(-3 * np.pi / 8), "Rz2": RZGate(np.pi / 2), "Ry1": RYGate(np.pi / 2) } return unitary_gates[gate_name]