def test_gate_angle(): gate0 = qf.random_gate(1) gate1 = qf.random_gate(1) qf.gate_angle(gate0, gate1) assert not qf.gates_close(gate0, gate1) assert qf.gates_close(gate0, gate0)
def test_fubini_study_angle(): for _ in range(REPS): theta = random.uniform(-pi, +pi) ang = qf.asarray(qf.fubini_study_angle(qf.I().vec, qf.RX(theta).vec)) assert 2 * ang / abs(theta) == ALMOST_ONE ang = qf.asarray(qf.fubini_study_angle(qf.I().vec, qf.RY(theta).vec)) assert 2 * ang / abs(theta) == ALMOST_ONE ang = qf.asarray(qf.fubini_study_angle(qf.I().vec, qf.RZ(theta).vec)) assert 2 * ang / abs(theta) == ALMOST_ONE ang = qf.asarray( qf.fubini_study_angle(qf.SWAP().vec, qf.PSWAP(theta).vec)) assert 2 * ang / abs(theta) == ALMOST_ONE ang = qf.asarray(qf.fubini_study_angle(qf.I().vec, qf.PHASE(theta).vec)) assert 2 * ang / abs(theta) == ALMOST_ONE for n in range(1, 6): eye = qf.identity_gate(n) assert qf.asarray(qf.fubini_study_angle(eye.vec, eye.vec)) \ == ALMOST_ZERO with pytest.raises(ValueError): qf.fubini_study_angle(qf.random_gate(1).vec, qf.random_gate(2).vec)
def test_decomp_sqrtswap_sandwich(): circ0 = qf.Circuit() circ0 += qf.CANONICAL(1 / 4, 1 / 4, 1 / 4, 0, 1) circ0 += qf.random_gate([0]) circ0 += qf.random_gate([1]) circ0 += qf.CANONICAL(1 / 4, 1 / 4, 1 / 4, 0, 1) gate0 = circ0.asgate() circ1 = qf.canonical_decomposition(gate0) gate1 = circ1.asgate() assert qf.gates_close(gate0, gate1)
def test_inverse_random(): K = 4 for _ in range(REPS): gate = qf.random_gate(K) inv = gate.H gate = inv @ gate assert qf.gates_close(qf.identity_gate(4), gate)
def sandwich_decompositions(coords0, coords1, samples=SAMPLES): """Create composite gates, decompose, and return a list of canonical coordinates""" decomps = [] for _ in range(samples): circ = qf.Circuit() circ += qf.CANONICAL(*coords0, 0, 1) circ += qf.random_gate([0]) circ += qf.random_gate([1]) circ += qf.CANONICAL(*coords1, 0, 1) gate = circ.asgate() coords = qf.canonical_coords(gate) decomps.append(coords) return decomps
def test_bloch_decomposition(): theta = 1.23 gate0 = qf.RN(theta, 1, 0, 0) gate1 = qf.bloch_decomposition(gate0).elements[0] assert qf.gates_close(gate0, gate1) gate0 = qf.RN(theta, 0, 1, 0) gate1 = qf.bloch_decomposition(gate0).elements[0] assert qf.gates_close(gate0, gate1) gate0 = qf.RN(theta, 0, 0, 1) gate1 = qf.bloch_decomposition(gate0).elements[0] assert qf.gates_close(gate0, gate1) gate0 = qf.RN(pi, np.sqrt(2), 0, np.sqrt(2)) gate1 = qf.bloch_decomposition(gate0).elements[0] assert qf.gates_close(gate0, gate1) for _ in range(REPS): gate0 = qf.random_gate(qubits=[0]) gate1 = qf.bloch_decomposition(gate0).elements[0] assert qf.gates_close(gate0, gate1) gate0 = qf.I(0) gate1 = qf.bloch_decomposition(gate0).elements[0] assert qf.gates_close(gate0, gate1)
def main(): """CLI""" print(fit_zyz.__doc__) print('Fitting randomly selected 1-qubit gate...') target_gate = qf.random_gate(1) t = fit_zyz(target_gate) print('Fitted parameters:', t)
def test_kronecker_decomposition(): for _ in range(REPS): left = qf.random_gate(1).vec.asarray() right = qf.random_gate(1).vec.asarray() both = np.kron(left, right) gate0 = qf.Gate(both, qubits=[0, 1]) circ = qf.kronecker_decomposition(gate0) gate1 = circ.asgate() assert qf.gates_close(gate0, gate1) circ0 = qf.Circuit() circ0 += qf.Z(0) circ0 += qf.H(1) gate0 = circ.asgate() circ1 = qf.kronecker_decomposition(gate0) gate1 = circ1.asgate() assert qf.gates_close(gate0, gate1)
def test_entropy(): N = 4 rho0 = qf.mixed_density(N) ent = qf.entropy(rho0, base=2) assert np.isclose(ent, N) # Entropy invariant to unitary evolution chan = qf.random_gate(N).aschannel() rho1 = chan.evolve(rho0) ent = qf.entropy(rho1, base=2) assert np.isclose(ent, N)
def test_canonical_decomp_sandwich(): for _ in range(REPS): # Random CZ sandwich circ0 = qf.Circuit() circ0 += qf.random_gate([0]) circ0 += qf.random_gate([1]) circ0 += qf.CZ(0, 1) circ0 += qf.TY(0.4, 0) circ0 += qf.TY(0.25, 1) circ0 += qf.CZ(0, 1) circ0 += qf.random_gate([0]) circ0 += qf.random_gate([1]) gate0 = circ0.asgate() circ1 = qf.canonical_decomposition(gate0) gate1 = circ1.asgate() assert qf.gates_close(gate0, gate1) assert qf.almost_unitary(gate0)
def test_mutual_info(): rho0 = qf.mixed_density(4) info0 = qf.mutual_info(rho0, qubits0=[0, 1], qubits1=[2, 3]) # Information invariant to local unitary evolution chan = qf.random_gate(2).aschannel() rho1 = chan.evolve(rho0) info1 = qf.mutual_info(rho1, qubits0=[0, 1], qubits1=[2, 3]) assert np.isclose(info0, info1) info2 = qf.mutual_info(rho1, qubits0=[0, 1]) assert np.isclose(info0, info2)
def test_repr(): g = qf.H() assert str(g) == 'H 0' g = qf.RX(3.12) assert str(g) == 'RX(3.12) 0' g = qf.identity_gate(2) assert str(g) == 'I 0 1' g = qf.random_gate(4) assert str(g) == 'RAND4 0 1 2 3' g = qf.H(0) assert str(g) == 'H 0' g = qf.CNOT(0, 1) assert str(g) == 'CNOT 0 1' g = qf.Gate(qf.CNOT().tensor) assert str(g).startswith('<quantumflow.ops.Gate')
def test_random_gate(): for n in range(1, 3): for _ in range(REPS): assert qf.almost_unitary(qf.random_gate(n))
def test_vectors_not_close(): assert not qf.vectors_close(qf.random_gate(1).vec, qf.random_gate(2).vec) assert not qf.vectors_close(qf.zero_state(1).vec, qf.random_gate(1).vec) assert not qf.vectors_close(qf.X(0).vec, qf.X(1).vec)
def test_canonical_decomp_random(): for _ in range(REPS * 2): gate0 = qf.random_gate([0, 1]) gate1 = qf.canonical_decomposition(gate0).asgate() assert qf.gates_close(gate0, gate1)
def test_fit_zyz_eager(): import examples.eager_fit_gate as ex target_gate = qf.random_gate(1) t = ex.fit_zyz(target_gate) print(t)
def test_fit_zyz(): import examples.tensorflow_fit_gate as ex target_gate = qf.random_gate(1) t = ex.fit_zyz(target_gate) print(t)
def test_gatepow(): gates = [ qf.I(), qf.X(), qf.Y(), qf.Z(), qf.H(), qf.S(), qf.T(), qf.PHASE(0.1), qf.RX(0.2), qf.RY(0.3), qf.RZ(0.4), qf.CZ(), qf.CNOT(), qf.SWAP(), qf.ISWAP(), qf.CPHASE00(0.5), qf.CPHASE01(0.6), qf.CPHASE10(0.6), qf.CPHASE(0.7), qf.PSWAP(0.15), qf.CCNOT(), qf.CSWAP(), qf.TX(2.7), qf.TY(1.2), qf.TZ(0.3), qf.ZYZ(3.5, 0.9, 2.1), qf.CANONICAL(0.1, 0.2, 7.4), qf.XX(1.8), qf.YY(0.9), qf.ZZ(0.45), qf.PISWAP(0.2), qf.EXCH(0.1), qf.TH(0.3) ] for gate in gates: assert qf.gates_close(gate.H, gate**-1) for gate in gates: sqrt_gate = gate**(1 / 2) two_gate = sqrt_gate @ sqrt_gate assert qf.gates_close(gate, two_gate) for gate in gates: gate0 = gate**0.3 gate1 = gate**0.7 gate2 = gate0 @ gate1 assert qf.gates_close(gate, gate2) for K in range(1, 5): gate = qf.random_gate(K) # FIXME: Throw error on K=0 sqrt_gate = gate**0.5 two_gate = sqrt_gate @ sqrt_gate assert qf.gates_close(gate, two_gate) for gate in gates: rgate = qf.Gate((gate**0.5).tensor) tgate = rgate @ rgate assert qf.gates_close(gate, tgate)
def test_zyz_decomposition(): gate0 = qf.random_gate(1) circ1 = qf.zyz_decomposition(gate0) gate1 = circ1.asgate() assert qf.gates_close(gate0, gate1)