def test_basic_gates(): I = sympy.I cos = sympy.cos sin = sympy.sin exp = sympy.exp BS = QubitWaveFunction.from_int angle = sympy.pi gates = [ X(0), Y(0), Z(0), Rx(target=0, angle=angle), Ry(target=0, angle=angle), Rz(target=0, angle=angle), H(0) ] results = [ BS(1), I * BS(1), BS(0), cos(-angle / 2) * BS(0) + I * sin(-angle / 2) * BS(1), cos(-angle / 2) * BS(0) + I * sin(-angle / 2) * I * BS(1), exp(-I * angle / 2) * BS(0), 1 / sympy.sqrt(2) * (BS(0) + BS(1)) ] for i, g in enumerate(gates): wfn = simulate(g, backend="symbolic", variables={angle: sympy.pi}) assert (wfn == strip_sympy_zeros(results[i]))
def test_moments(): c = QCircuit() c += CNOT(target=0, control=(1, 2, 3)) c += H(target=[0, 1]) c += Rx(angle=numpy.pi, target=[0, 3]) c += Z(target=1) c += Phase(phi=numpy.pi, target=4) moms = c.moments assert len(moms) == 3 assert (moms[0].gates[1].parameter == assign_variable(numpy.pi)) assert (moms[0].gates[1].target == (4, ))
def test_circuit_from_moments(): c = QCircuit() c += CNOT(target=0, control=(1, 2, 3)) c += Phase(phi=numpy.pi, target=4) c += Rx(angle=Variable('a'), target=[0, 3]) c += H(target=[0, 1]) c += Rx(angle=Variable('a'), target=[2, 3]) ## table[1] should equal 1 at this point, len(moments should be 3) c += Z(target=1) c += Rx(angle=Variable('a'), target=[0, 3]) moms = c.moments c2 = QCircuit.from_moments(moms) assert c == c2
def test_canonical_moments(): c = QCircuit() c += CNOT(target=0, control=(1, 2, 3)) c += Rx(angle=Variable('a'), target=[0, 3]) c += H(target=[0, 1]) c += Rx(angle=Variable('a'), target=[2, 3]) c += Rx(angle=Variable('a'), target=[0, 3]) c += Z(target=1) c += Phase(phi=numpy.pi, target=4) moms = c.canonical_moments assert len(moms) == 6 assert (moms[0].gates[1].parameter == assign_variable(numpy.pi)) assert (moms[0].gates[1].target == (4, )) assert hasattr(moms[3].gates[0], 'axis') assert len(moms[0].qubits) == 5
def compile_phase_to_z(gate) -> QCircuit: """ Compile phase gate to parametrized Z gate. Parameters ---------- gate: the gate. Returns ------- QCircuit, the result of compilation. """ if not isinstance(gate, PhaseGateImpl): return QCircuit.wrap_gate(gate) phase = gate.parameter return Z(power=phase / pi, target=gate.target, control=gate.control)
def test_consistency(): angle = numpy.pi / 2 cpairs = [(CNOT(target=0, control=1), X(target=0, control=1)), (Ry(target=0, angle=numpy.pi), Rz(target=0, angle=4 * numpy.pi) + X(target=0)), (Rz(target=0, angle=numpy.pi), Rz(target=0, angle=numpy.pi) + Z(target=0)), (Rz(target=0, angle=angle), Rz(target=0, angle=angle / 2) + Rz(target=0, angle=angle / 2)), (Rx(target=0, angle=angle), Rx(target=0, angle=angle / 2) + Rx(target=0, angle=angle / 2)), (Ry(target=0, angle=angle), Ry(target=0, angle=angle / 2) + Ry(target=0, angle=angle / 2))] for c in cpairs: print("circuit=", c[0], "\n", c[1]) wfn1 = simulate(c[0], backend="symbolic") wfn2 = simulate(c[1], backend="symbolic") assert (numpy.isclose(wfn1.inner(wfn2), 1.0))
def compile_ch(gate: QGateImpl) -> QCircuit: """ Compile CH gates into its equivalent: CH = Ry(0.25pi) CZ Ry(-0.25pi) Parameters ---------- gate: the gate. Returns ------- QCircuit, the result of compilation. """ if gate.name.lower() == "h" and gate.is_controlled(): return Ry(target=gate.target, control=None, angle=-numpy.pi / 4) \ + Z(target=gate.target, control=gate.control, power=gate.power if gate.is_parametrized() else None) \ + Ry(target=gate.target, control=None, angle=numpy.pi / 4) else: return QCircuit.wrap_gate(gate)
c += Phase(phi=numpy.pi, target=4) c += Rx(angle=Variable('a'), target=[0, 3]) c += H(target=[0, 1]) c += Rx(angle=Variable('a'), target=[2, 3]) ## table[1] should equal 1 at this point, len(moments should be 3) c += Z(target=1) c += Rx(angle=Variable('a'), target=[0, 3]) moms = c.moments c2 = QCircuit.from_moments(moms) assert c == c2 @pytest.mark.parametrize( "gate, angle", [ (Z(target=0, control=None), numpy.pi), # Z = u1(pi) (Z(target=0, control=1), numpy.pi), (S(target=0, control=None), numpy.pi / 2), # S = u1(pi/2) (S(target=0, control=1), numpy.pi / 2), (S(target=0, control=None).dagger(), -numpy.pi / 2), # Sdg = u1(-pi/2) (S(target=0, control=1).dagger(), -numpy.pi / 2), (T(target=0, control=None), numpy.pi / 4), # T = u1(pi/4) (T(target=0, control=1), numpy.pi / 4), (T(target=0, control=None).dagger(), -numpy.pi / 4), # Tdg = u1(-pi/4) (T(target=0, control=1).dagger(), -numpy.pi / 4) ]) def test_unitary_gate_u1(gate, angle): """ Test some equivalences for u1 gate """ c_u1 = u1(lambd=angle,
def compile_phase_to_z(gate) -> QCircuit: if not isinstance(gate, PhaseGateImpl): return QCircuit.wrap_gate(gate) phase = gate.parameter return Z(power=phase / pi, target=gate.target, control=gate.control)