def test_circuits_close() -> None: circ0 = qf.Circuit([qf.H(0)]) circ1 = qf.Circuit([qf.H(2)]) assert not qf.circuits_close(circ0, circ1) circ2 = qf.Circuit([qf.X(0)]) assert not qf.circuits_close(circ0, circ2) circ3 = qf.Circuit([qf.H(0)]) assert qf.circuits_close(circ0, circ3)
def test_PauliGate() -> None: pauli0 = 0.5 * np.pi * qf.sX(0) * qf.sX(1) alpha = 0.4 circ = qf.PauliGate(pauli0, alpha) coords = qf.canonical_coords(circ.asgate()) assert np.isclose(coords[0], 0.4) pauli1 = np.pi * qf.sX(0) * qf.sX(1) * qf.sY(2) * qf.sZ(3) _ = qf.PauliGate(pauli1, alpha) top2 = nx.star_graph(4) pauli2 = 0.5 * np.pi * qf.sX(1) * qf.sY(2) * qf.sZ(3) _ = qf.PauliGate(pauli2, alpha).decompose(top2) alpha = 0.2 top3 = nx.star_graph(4) pauli3 = 0.5 * np.pi * qf.sX(1) * qf.sX(2) circ3 = qf.Circuit(qf.PauliGate(pauli3, alpha).decompose(top3)) assert qf.circuits_close(circ3, qf.Circuit([qf.I(0), qf.XX(alpha, 1, 2)])) qf.PauliGate(qf.sI(0), alpha).decompose(top2) with pytest.raises(ValueError): pauli4 = 0.5j * np.pi * qf.sX(1) * qf.sX(2) _ = qf.Circuit(qf.PauliGate(pauli4, alpha).decompose(top3)) top4 = nx.DiGraph() nx.add_path(top4, [3, 2, 1, 0]) _ = qf.Circuit(qf.PauliGate(pauli3, alpha).decompose(top4))
def test_CH() -> None: gate1 = qf.CH(0, 1) # I picked up this circuit for a CH gate from qiskit # qiskit/extensions/standard/ch.py # But it clearly far too long. CH is locally equivalent to CNOT, # so requires only one CNOT gate. circ2 = qf.Circuit([ qf.H(1), qf.S_H(1), qf.CNot(0, 1), qf.H(1), qf.T(1), qf.CNot(0, 1), qf.T(1), qf.H(1), qf.S(1), qf.X(1), qf.S(0), ]) assert qf.gates_close(gate1, circ2.asgate()) # Here's a better decomposition circ1 = qf.Circuit([qf.YPow(+0.25, 1), qf.CNot(0, 1), qf.YPow(-0.25, 1)]) assert qf.gates_close(gate1, circ1.asgate()) assert qf.circuits_close(circ1, circ2)
def test_circuit_to_pyquil() -> None: circ = qf.Circuit() circ += qf.X(0) prog = xforest.circuit_to_pyquil(circ) assert str(prog) == "X 0\n" circ = qf.Circuit() circ1 = qf.Circuit() circ2 = qf.Circuit() circ1 += qf.Ry(np.pi / 2, 0) circ1 += qf.Rz(np.pi, 0) circ1 += qf.Ry(np.pi / 2, 1) circ1 += qf.Rx(np.pi, 1) circ1 += qf.CNot(0, 1) circ2 += qf.Rx(-np.pi / 2, 1) circ2 += qf.Ry(4.71572463191, 1) circ2 += qf.Rx(np.pi / 2, 1) circ2 += qf.CNot(0, 1) circ2 += qf.Rx(-2 * 2.74973750579, 0) circ2 += qf.Rx(-2 * 2.74973750579, 1) circ += circ1 circ += circ2 prog = xforest.circuit_to_pyquil(circ) new_circ = xforest.pyquil_to_circuit(prog) assert qf.circuits_close(circ, new_circ)
def test_multiswapgate() -> None: # Should be same as a swap. perm0 = qf.MultiSwapGate([0, 1], [1, 0]) gate0 = qf.Swap(0, 1) assert qf.gates_close(perm0.asgate(), gate0) assert qf.gates_close(perm0.asgate(), perm0.H.asgate()) perm1 = qf.MultiSwapGate.from_gates(qf.Circuit([gate0])) assert qf.gates_close(perm0.asgate(), perm1.asgate()) perm2 = qf.MultiSwapGate.from_gates(qf.Circuit([perm1])) assert qf.gates_close(perm0, perm2) with pytest.raises(ValueError): qf.MultiSwapGate.from_gates(qf.Circuit(qf.CNot(0, 1))) N = 8 qubits_in = list(range(N)) qubits_out = np.random.permutation(qubits_in) permN = qf.MultiSwapGate(qubits_in, qubits_out) assert qf.gates_close(perm0.asgate(), perm1.asgate()) iden = qf.Circuit([permN, permN.H]) assert qf.almost_identity(iden.asgate()) assert qf.circuits_close(iden, qf.Circuit([qf.IdentityGate(qubits_in)])) swaps = qf.Circuit(permN.decompose()) # Add identity so we don't lose qubits swaps += qf.IdentityGate(permN.qubits_in) permN2 = qf.MultiSwapGate.from_gates(swaps) assert qf.circuits_close(swaps, qf.Circuit([permN])) assert qf.circuits_close(swaps, qf.Circuit([permN2])) assert qf.circuits_close(qf.Circuit([permN]), qf.Circuit([permN2])) with pytest.raises(ValueError): _ = qf.MultiSwapGate([0, 1], [1, 2]) # Channels assert qf.channels_close(perm0.aschannel(), gate0.aschannel()) rho0 = qf.random_state([0, 1, 3]).asdensity() rho1 = perm0.evolve(rho0) rho2 = gate0.aschannel().evolve(rho0) assert qf.densities_close(rho1, rho2)
def test_compile() -> None: circ0 = qf.addition_circuit([0], [1], [2, 3]) circ1 = qf.compile_circuit(circ0) assert qf.circuits_close(circ0, circ1) assert circ1.size() == 76 dagc = qf.DAGCircuit(circ1) assert dagc.depth(local=False) == 16 counts = qf.count_operations(dagc) assert counts[qf.ZPow] == 27 assert counts[qf.XPow] == 32 assert counts[qf.CZ] == 17
def test_rotatequbits() -> None: rev = qf.CircularShiftGate([0, 1, 2, 3, 4], 2) perm = qf.MultiSwapGate([0, 1, 2, 3, 4], [2, 3, 4, 0, 1]) assert qf.circuits_close(qf.Circuit(rev.decompose()), qf.Circuit(perm.decompose()))
def test_reversequbits() -> None: rev = qf.ReversalGate([0, 1, 2, 3, 4]) perm = qf.MultiSwapGate([0, 1, 2, 3, 4], [4, 3, 2, 1, 0]) assert qf.circuits_close(qf.Circuit(rev.decompose()), qf.Circuit(perm.decompose()))
def test_circuit_to_cirq_unitary() -> None: gate0 = qf.RandomGate([4, 5]) circ0 = qf.Circuit([gate0]) cqc = circuit_to_cirq(circ0) circ1 = cirq_to_circuit(cqc) assert qf.circuits_close(circ0, circ1)
def test_circuit_to_circ_translate() -> None: circ0 = qf.Circuit([qf.Can(0.2, 0.3, 0.1, 0, 1)]) cqc = circuit_to_cirq(circ0, translate=True) circ2 = cirq_to_circuit(cqc) assert qf.circuits_close(circ0, circ2)