def test_measurement(): rho = qf.zero_state(2).asdensity() chan = qf.H(0).aschannel() rho = chan.evolve(rho) rho = qf.Kraus([qf.P0(0), qf.P1(0)]).aschannel().evolve(rho) K = qf.Kraus([qf.P0(1), qf.P1(1)]) chan = K.aschannel() rho = qf.Kraus([qf.P0(1), qf.P1(1)]).aschannel().evolve(rho) prob = qf.asarray(rho.probabilities()) assert np.allclose(prob, [[0.5, 0], [0.5, 0]]) assert prob[0, 0] * 2 == ALMOST_ONE assert prob[1, 0] * 2 == ALMOST_ONE
def test_measurement() -> None: rho = qf.zero_state(2).asdensity() chan = qf.H(0).aschannel() rho = chan.evolve(rho) rho = qf.Kraus([qf.P0(0), qf.P1(0)]).aschannel().evolve(rho) K = qf.Kraus([qf.P0(1), qf.P1(1)]) _ = K.aschannel() rho = qf.Kraus([qf.P0(1), qf.P1(1)]).aschannel().evolve(rho) prob = rho.probabilities() assert np.allclose(prob, [[0.5, 0], [0.5, 0]]) assert np.isclose(prob[0, 0] * 2, 1.0) assert np.isclose(prob[1, 0] * 2, 1.0)
def test_gates_close(): assert not qf.gates_close(qf.I(), qf.identity_gate(2)) assert qf.gates_close(qf.I(), qf.I()) assert qf.gates_close(qf.P0(), qf.P0()) assert not qf.gates_close(qf.P0(), qf.P1()) assert qf.gates_close(qf.CNOT(0, 1), qf.CNOT(0, 1)) assert not qf.gates_close(qf.CNOT(1, 0), qf.CNOT(0, 1))
def test_gates_to_latex(): circ = qf.Circuit() circ += qf.I(7) circ += qf.X(0) circ += qf.Y(1) circ += qf.Z(2) circ += qf.H(3) circ += qf.S(4) circ += qf.T(5) circ += qf.S_H(6) circ += qf.T_H(7) circ += qf.RX(-0.5*pi, 0) circ += qf.RY(0.5*pi, 1) circ += qf.RZ((1/3)*pi, 1) circ += qf.RY(0.222, 1) circ += qf.TX(0.5, 0) circ += qf.TY(0.5, 1) circ += qf.TZ(0.4, 1) circ += qf.TZ(0.47276, 1) # Gate with cunning hack gate = qf.RZ(0.4, 1) gate.params['theta'] = qf.Parameter('\\theta') circ += gate circ += qf.CNOT(1, 2) circ += qf.CNOT(2, 1) circ += qf.CZ(1, 3) circ += qf.SWAP(1, 5) circ += qf.ISWAP(4, 2) # circ += qf.Barrier(0, 1, 2, 3, 4, 5, 6) # Not yet supported circ += qf.CCNOT(1, 2, 3) circ += qf.CSWAP(4, 5, 6) circ += qf.P0(0) circ += qf.P1(1) circ += qf.Reset(2) circ += qf.Reset(4, 5, 6) circ += qf.H(4) # circ += qf.Reset() # FIXME. Should fail with clear error message circ += qf.XX(0.25, 1, 3) circ += qf.YY(0.75, 1, 3) circ += qf.ZZ(1/3, 3, 1) circ += qf.Measure(0) latex = qf.circuit_to_latex(circ) print(latex)
def test_projectors() -> None: ket = qf.zero_state(1) assert qf.P0(0).run(ket).norm() == 1.0 ket = qf.H(0).run(ket) measure0 = qf.P0(0).run(ket) assert np.isclose(measure0.norm(), 0.5) measure1 = qf.P1(0).run(ket) assert np.isclose(measure1.norm(), 0.5)
def test_projectors(): ket = qf.zero_state(1) assert qf.asarray(qf.P0(0).run(ket).norm()) == 1.0 ket = qf.H(0).run(ket) measure0 = qf.P0(0).run(ket) assert qf.asarray(measure0.norm()) * 2 == ALMOST_ONE measure1 = qf.P1(0).run(ket) assert qf.asarray(measure1.norm()) * 2 == ALMOST_ONE
def test_not_unitary() -> None: assert not qf.almost_unitary(qf.P0()) assert not qf.almost_unitary(qf.P1())
def test_visualize_circuit() -> None: circ = qf.Circuit() circ += qf.I(7) circ += qf.X(0) circ += qf.Y(1) circ += qf.Z(2) circ += qf.H(3) circ += qf.S(4) circ += qf.T(5) circ += qf.S_H(6) circ += qf.T_H(7) circ += qf.Rx(-0.5 * pi, 0) circ += qf.Ry(0.5 * pi, 4) circ += qf.Rz((1 / 3) * pi, 5) circ += qf.Ry(0.222, 6) circ += qf.XPow(0.5, 0) circ += qf.YPow(0.5, 2) circ += qf.ZPow(0.4, 2) circ += qf.HPow(0.5, 3) circ += qf.ZPow(0.47276, 1) # Gate with symbolic parameter # gate = qf.Rz(Symbol('\\theta'), 1) # circ += gate circ += qf.CNot(1, 2) circ += qf.CNot(2, 1) # circ += qf.IDEN(*range(8)) circ += qf.ISwap(4, 2) circ += qf.ISwap(6, 5) circ += qf.CZ(1, 3) circ += qf.Swap(1, 5) # circ += qf.Barrier(0, 1, 2, 3, 4, 5, 6) # Not yet supported in latex circ += qf.CCNot(1, 2, 3) circ += qf.CSwap(4, 5, 6) circ += qf.P0(0) circ += qf.P1(1) circ += qf.Reset(2) circ += qf.Reset(4, 5, 6) circ += qf.H(4) circ += qf.XX(0.25, 1, 4) circ += qf.XX(0.25, 1, 2) circ += qf.YY(0.75, 1, 3) circ += qf.ZZ(1 / 3, 3, 1) circ += qf.CPhase(0, 0, 1) circ += qf.CPhase(pi * 1 / 2, 0, 4) circ += qf.Can(1 / 3, 1 / 2, 1 / 2, 0, 1) circ += qf.Can(1 / 3, 1 / 2, 1 / 2, 2, 4) circ += qf.Can(1 / 3, 1 / 2, 1 / 2, 6, 5) # circ += qf.Measure(0) # circ += qf.Measure(1, 1) circ += qf.PSwap(pi / 2, 6, 7) circ += qf.Ph(1 / 4, 7) circ += qf.CH(1, 6) circ += qf.visualization.NoWire([0, 1, 2]) # circ += qf.visualization.NoWire(4, 1, 2) if os.environ.get("QF_VIZTEST"): print() print(qf.circuit_to_diagram(circ)) qf.circuit_to_diagram(circ) qf.circuit_to_latex(circ) qf.circuit_to_latex(circ, package="qcircuit") qf.circuit_to_latex(circ, package="quantikz") qf.circuit_to_diagram(circ) qf.circuit_to_diagram(circ, use_unicode=False) latex = qf.circuit_to_latex(circ, package="qcircuit") print(latex) if os.environ.get("QF_VIZTEST"): qf.latex_to_image(latex).show() latex = qf.circuit_to_latex(circ, package="quantikz") print(latex) if os.environ.get("QF_VIZTEST"): qf.latex_to_image(latex).show()
def test_kruas_qubits(): rho = qf.Kraus([qf.P0(0), qf.P1(1)]) assert rho.qubits == (0, 1) assert rho.qubit_nb == 2