mitodl / mitx-grading-library

The MITx Grading Library, a python grading library for edX
https://edge.edx.org/courses/course-v1:MITx+grading-library+examples/
BSD 3-Clause "New" or "Revised" License
14 stars 9 forks source link

Quantum arrays #185

Closed ChristopherChudzicki closed 5 years ago

ChristopherChudzicki commented 5 years ago

This is meant as a proof-of-concept customizing MathArray error messages, as described in #183.

Here's an example:

from mitxgraders.plugins.quantum_matharray import QuantumMathArray
from mitxgraders import MatrixGrader, RealMatrices, DependentSampler

grader = MatrixGrader(
    variables=['ket0', 'ket1', 'bra0', 'bra1'],
    sample_from={
        'ket0': RealMatrices(shape=(2, 1), array_class=QuantumMathArray),
        'ket1': RealMatrices(shape=(2, 1), array_class=QuantumMathArray),
        'bra0': DependentSampler(depends=['ket0'], formula='adj(ket0)'),
        'bra1': DependentSampler(depends=['ket1'], formula='adj(ket1)')
    }
)

grader('ket0*bra1', 'ket0 + bra1')
# MathArrayShapeError: Cannot add/subtract a ket with a bra.

# grader('ket0*bra1', 'ket0*bra1 + 1')
# MathArrayShapeError: Cannot add/subtract scalars to a operator.

# grader('ket0*bra1', 'sin(ket0*bra1)')
# DomainError: There was an error evaluating function sin(...)
# 1st input has an error: received a operator, expected a scalar

# grader('ket0*bra1', 'ket0*adj(ket1)')
# {'grade_decimal': 1, 'msg': '', 'ok': True}

The code is pretty simpler: just a QuantumMathArray is a sublcass of MathArray with customized shape_name and description properties.

@jolyonb Let me know what you think. Maybe worthwhile as a plugin for 8.06? If you decide to use it, the only change I would want to make is refactor slightly the matrix samplers to all use array_class consistently. (And, of course, use whatever names you actually want.)

jolyonb commented 5 years ago

Hey, this is actually really neat! I like it! It would require some refactoring to get into the 8.04/5/6 courses as they've basically all been built at this stage, but I can definitely see it being used. I'll let you flesh it out a bit more before I comment fully on the code.

(The use case I had for noncommutative variables was actually different - I was setting up a variable called nabla to represent a gradient operator.)

jolyonb commented 5 years ago

We decided that this was a) too much work, and b) too hard for a typical user to make use of, so I'm closing this.