def test_resolve(resolve_fn): diagonal_angles = [2, 3, 5, 7, 11, 13, 17, 19] diagonal_gate = cirq.ThreeQubitDiagonalGate( diagonal_angles[:6] + [sympy.Symbol('a'), sympy.Symbol('b')] ) assert cirq.is_parameterized(diagonal_gate) diagonal_gate = resolve_fn(diagonal_gate, {'a': 17}) assert diagonal_gate == cirq.ThreeQubitDiagonalGate(diagonal_angles[:7] + [sympy.Symbol('b')]) assert cirq.is_parameterized(diagonal_gate) diagonal_gate = resolve_fn(diagonal_gate, {'b': 19}) assert diagonal_gate == cirq.ThreeQubitDiagonalGate(diagonal_angles) assert not cirq.is_parameterized(diagonal_gate)
def test_diagram(): a, b, c, d = cirq.LineQubit.range(4) circuit = cirq.Circuit( cirq.TOFFOLI(a, b, c), cirq.TOFFOLI(a, b, c)**0.5, cirq.CCX(a, c, b), cirq.CCZ(a, d, b), cirq.CCZ(a, d, b)**0.5, cirq.CSWAP(a, c, d), cirq.FREDKIN(a, b, c), ) cirq.testing.assert_has_diagram( circuit, """ 0: ───@───@───────@───@───@───────@───@─── │ │ │ │ │ │ │ 1: ───@───@───────X───@───@───────┼───×─── │ │ │ │ │ │ │ 2: ───X───X^0.5───@───┼───┼───────×───×─── │ │ │ 3: ───────────────────@───@^0.5───×─────── """, ) cirq.testing.assert_has_diagram( circuit, """ 0: ---@---@-------@---@---@-------@------@------ | | | | | | | 1: ---@---@-------X---@---@-------|------swap--- | | | | | | | 2: ---X---X^0.5---@---|---|-------swap---swap--- | | | 3: -------------------@---@^0.5---swap---------- """, use_unicode_characters=False, ) diagonal_circuit = cirq.Circuit( cirq.ThreeQubitDiagonalGate([2, 3, 5, 7, 11, 13, 17, 19])(a, b, c)) cirq.testing.assert_has_diagram( diagonal_circuit, """ 0: ───diag(2, 3, 5, 7, 11, 13, 17, 19)─── │ 1: ───#2───────────────────────────────── │ 2: ───#3───────────────────────────────── """, ) cirq.testing.assert_has_diagram( diagonal_circuit, """ 0: ---diag(2, 3, 5, 7, 11, 13, 17, 19)--- | 1: ---#2--------------------------------- | 2: ---#3--------------------------------- """, use_unicode_characters=False, )
def test_unitary(): assert cirq.has_unitary(cirq.CCX) np.testing.assert_allclose( cirq.unitary(cirq.CCX), np.array([ [1, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 1, 0], ]), atol=1e-8, ) assert cirq.has_unitary(cirq.CCX**0.5) np.testing.assert_allclose( cirq.unitary(cirq.CCX**0.5), np.array([ [1, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0.5 + 0.5j, 0.5 - 0.5j], [0, 0, 0, 0, 0, 0, 0.5 - 0.5j, 0.5 + 0.5j], ]), atol=1e-8, ) assert cirq.has_unitary(cirq.CCZ) np.testing.assert_allclose(cirq.unitary(cirq.CCZ), np.diag([1, 1, 1, 1, 1, 1, 1, -1]), atol=1e-8) assert cirq.has_unitary(cirq.CCZ**0.5) np.testing.assert_allclose(cirq.unitary(cirq.CCZ**0.5), np.diag([1, 1, 1, 1, 1, 1, 1, 1j]), atol=1e-8) assert cirq.has_unitary(cirq.CSWAP) np.testing.assert_allclose( cirq.unitary(cirq.CSWAP), np.array([ [1, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1], ]), atol=1e-8, ) diagonal_angles = [2, 3, 5, 7, 11, 13, 17, 19] assert cirq.has_unitary(cirq.ThreeQubitDiagonalGate(diagonal_angles)) np.testing.assert_allclose( cirq.unitary(cirq.ThreeQubitDiagonalGate(diagonal_angles)), np.diag([np.exp(1j * angle) for angle in diagonal_angles]), atol=1e-8, )
def test_diagonal_gate_property(): assert cirq.ThreeQubitDiagonalGate([2, 3, 5, 7, 0, 0, 0, 1]).diag_angles_radians == ((2, 3, 5, 7, 0, 0, 0, 1))
import sympy import cirq @pytest.mark.parametrize('eigen_gate_type', [cirq.CCXPowGate, cirq.CCZPowGate]) def test_eigen_gates_consistent_protocols(eigen_gate_type): cirq.testing.assert_eigengate_implements_consistent_protocols( eigen_gate_type) @pytest.mark.parametrize( 'gate', ( (cirq.CSWAP), (cirq.ThreeQubitDiagonalGate([2, 3, 5, 7, 11, 13, 17, 19])), (cirq.ThreeQubitDiagonalGate([0, 0, 0, 0, 0, 0, 0, 0])), (cirq.CCX), (cirq.CCZ), ), ) def test_consistent_protocols(gate): cirq.testing.assert_implements_consistent_protocols(gate) def test_init(): assert (cirq.CCZ**0.5).exponent == 0.5 assert (cirq.CCZ**0.25).exponent == 0.25 assert (cirq.CCX**0.5).exponent == 0.5 assert (cirq.CCX**0.25).exponent == 0.25
def test_cirq_qsim_all_supported_gates(): q0 = cirq.GridQubit(1, 1) q1 = cirq.GridQubit(1, 0) q2 = cirq.GridQubit(0, 1) q3 = cirq.GridQubit(0, 0) circuit = cirq.Circuit( cirq.Moment( cirq.H(q0), cirq.H(q1), cirq.H(q2), cirq.H(q3), ), cirq.Moment( cirq.T(q0), cirq.T(q1), cirq.T(q2), cirq.T(q3), ), cirq.Moment( cirq.CZPowGate(exponent=0.7, global_shift=0.2)(q0, q1), cirq.CXPowGate(exponent=1.2, global_shift=0.4)(q2, q3), ), cirq.Moment( cirq.XPowGate(exponent=0.3, global_shift=1.1)(q0), cirq.YPowGate(exponent=0.4, global_shift=1)(q1), cirq.ZPowGate(exponent=0.5, global_shift=0.9)(q2), cirq.HPowGate(exponent=0.6, global_shift=0.8)(q3), ), cirq.Moment( cirq.CX(q0, q2), cirq.CZ(q1, q3), ), cirq.Moment( cirq.X(q0), cirq.Y(q1), cirq.Z(q2), cirq.S(q3), ), cirq.Moment( cirq.XXPowGate(exponent=0.4, global_shift=0.7)(q0, q1), cirq.YYPowGate(exponent=0.8, global_shift=0.5)(q2, q3), ), cirq.Moment(cirq.I(q0), cirq.I(q1), cirq.IdentityGate(2)(q2, q3)), cirq.Moment( cirq.rx(0.7)(q0), cirq.ry(0.2)(q1), cirq.rz(0.4)(q2), cirq.PhasedXPowGate(phase_exponent=0.8, exponent=0.6, global_shift=0.3)(q3), ), cirq.Moment( cirq.ZZPowGate(exponent=0.3, global_shift=1.3)(q0, q2), cirq.ISwapPowGate(exponent=0.6, global_shift=1.2)(q1, q3), ), cirq.Moment( cirq.XPowGate(exponent=0.1, global_shift=0.9)(q0), cirq.YPowGate(exponent=0.2, global_shift=1)(q1), cirq.ZPowGate(exponent=0.3, global_shift=1.1)(q2), cirq.HPowGate(exponent=0.4, global_shift=1.2)(q3), ), cirq.Moment( cirq.SwapPowGate(exponent=0.2, global_shift=0.9)(q0, q1), cirq.PhasedISwapPowGate(phase_exponent=0.8, exponent=0.6)(q2, q3), ), cirq.Moment( cirq.PhasedXZGate(x_exponent=0.2, z_exponent=0.3, axis_phase_exponent=1.4)( q0 ), cirq.T(q1), cirq.H(q2), cirq.S(q3), ), cirq.Moment( cirq.SWAP(q0, q2), cirq.XX(q1, q3), ), cirq.Moment( cirq.rx(0.8)(q0), cirq.ry(0.9)(q1), cirq.rz(1.2)(q2), cirq.T(q3), ), cirq.Moment( cirq.YY(q0, q1), cirq.ISWAP(q2, q3), ), cirq.Moment( cirq.T(q0), cirq.Z(q1), cirq.Y(q2), cirq.X(q3), ), cirq.Moment( cirq.FSimGate(0.3, 1.7)(q0, q2), cirq.ZZ(q1, q3), ), cirq.Moment( cirq.ry(1.3)(q0), cirq.rz(0.4)(q1), cirq.rx(0.7)(q2), cirq.S(q3), ), cirq.Moment( cirq.IdentityGate(4).on(q0, q1, q2, q3), ), cirq.Moment( cirq.CCZPowGate(exponent=0.7, global_shift=0.3)(q2, q0, q1), ), cirq.Moment( cirq.CCXPowGate(exponent=0.4, global_shift=0.6)(q3, q1, q0).controlled_by( q2, control_values=[0] ), ), cirq.Moment( cirq.rx(0.3)(q0), cirq.ry(0.5)(q1), cirq.rz(0.7)(q2), cirq.rx(0.9)(q3), ), cirq.Moment( cirq.TwoQubitDiagonalGate([0.1, 0.2, 0.3, 0.4])(q0, q1), ), cirq.Moment( cirq.ThreeQubitDiagonalGate([0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.2, 1.3])( q1, q2, q3 ), ), cirq.Moment( cirq.CSwapGate()(q0, q3, q1), ), cirq.Moment( cirq.rz(0.6)(q0), cirq.rx(0.7)(q1), cirq.ry(0.8)(q2), cirq.rz(0.9)(q3), ), cirq.Moment( cirq.TOFFOLI(q3, q2, q0), ), cirq.Moment( cirq.FREDKIN(q1, q3, q2), ), cirq.Moment( cirq.MatrixGate( np.array( [ [0, -0.5 - 0.5j, -0.5 - 0.5j, 0], [0.5 - 0.5j, 0, 0, -0.5 + 0.5j], [0.5 - 0.5j, 0, 0, 0.5 - 0.5j], [0, -0.5 - 0.5j, 0.5 + 0.5j, 0], ] ) )(q0, q1), cirq.MatrixGate( np.array( [ [0.5 - 0.5j, 0, 0, -0.5 + 0.5j], [0, 0.5 - 0.5j, -0.5 + 0.5j, 0], [0, -0.5 + 0.5j, -0.5 + 0.5j, 0], [0.5 - 0.5j, 0, 0, 0.5 - 0.5j], ] ) )(q2, q3), ), cirq.Moment( cirq.MatrixGate(np.array([[1, 0], [0, 1j]]))(q0), cirq.MatrixGate(np.array([[0, -1j], [1j, 0]]))(q1), cirq.MatrixGate(np.array([[0, 1], [1, 0]]))(q2), cirq.MatrixGate(np.array([[1, 0], [0, -1]]))(q3), ), cirq.Moment( cirq.riswap(0.7)(q0, q1), cirq.givens(1.2)(q2, q3), ), cirq.Moment( cirq.H(q0), cirq.H(q1), cirq.H(q2), cirq.H(q3), ), ) simulator = cirq.Simulator() cirq_result = simulator.simulate(circuit) qsim_simulator = qsimcirq.QSimSimulator() qsim_result = qsim_simulator.simulate(circuit) assert cirq.linalg.allclose_up_to_global_phase( qsim_result.state_vector(), cirq_result.state_vector() )
import cirq @pytest.mark.parametrize('eigen_gate_type', [cirq.CCXPowGate, cirq.CCZPowGate]) def test_eigen_gates_consistent_protocols(eigen_gate_type): cirq.testing.assert_eigengate_implements_consistent_protocols( eigen_gate_type, ignoring_global_phase=True ) @pytest.mark.parametrize( 'gate,ignoring_global_phase', ( (cirq.CSWAP, False), (cirq.ThreeQubitDiagonalGate([2, 3, 5, 7, 11, 13, 17, 19]), True), (cirq.ThreeQubitDiagonalGate([0, 0, 0, 0, 0, 0, 0, 0]), True), (cirq.CCX, False), (cirq.CCZ, False), ), ) def test_consistent_protocols(gate, ignoring_global_phase): cirq.testing.assert_implements_consistent_protocols( gate, ignoring_global_phase=ignoring_global_phase ) def test_init(): assert (cirq.CCZ**0.5).exponent == 0.5 assert (cirq.CCZ**0.25).exponent == 0.25 assert (cirq.CCX**0.5).exponent == 0.5